Friday, January 13, 2006

Fun Java Fact #1

Have you ever wanted more solutions for your program? Say the pragram is to be run as an applet but the user would like to download it and use it outside a browser. I was thinking over how to do this so i made a simple program that operates as a applet and a application. This can be very usefull because users can use your program out of a browser when they are not online. It does not take very much to do this. Just a little flexability. The program is not a tough one at all; just a JButton with a label saying how many times the JButton has been clicked. Also note it is a Swing applet/application. Here is my code:
/*
* SwingAppletAndApplication.java
* Created on: Friday January 13, 2006 at 9:10
*
* @author Rekahsoft.blogspot.com
* @programmer Collin Doering
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

public class SwingAppletAndApplication extends JApplet implements java.awt.event.ActionListener {

//create the components to be used in the applet/application
private JButton btn;
private JLabel label;
//create a variable to keep track of the amount of button clicks
private int timesClicked = 0;

boolean DEBUG = true;

//create a constructor so that the main method can create an istance of it because it can not call the init method because it is not static
public SwingAppletAndApplication() { //default constructor
//init variables
btn = new JButton("Click me");
btn.addActionListener(this); //add an action listener to the JButton
label = new JLabel("I've been clicked: " + timesClicked + " times!!");

// Lay out the GUI (using box layout)
Container contentPane = getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
contentPane.add(label);
contentPane.add(btn);
label.setAlignmentX(Component.LEFT_ALIGNMENT);
btn.setAlignmentX(Component.RIGHT_ALIGNMENT);
}

//init method overrode from java.applet.Applet
public void init() {
new SwingAppletAndApplication(); //show the gui in a applet
}

// Action method -- executed when the user clicks the button.
public void actionPerformed(ActionEvent e) {
timesClicked++;
label.setText("I've been clicked: " + timesClicked + " times!!");
}

//main method - the starting point in a application for the JVM
public static void main(String args[]) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Application form"); //create a JFrame to show the
frame.getContentPane().add(new SwingAppletAndApplication()); //Add the GUI to the application
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true); //set the JFrame visible
}
});
}

}

I explain the code with coments through out it (sorry i could not format it so it looks nice). Give me some feedback on what you think and if you already knew you could do this. I thaught it was pretty cool because i had never heard of this.

1 comment:

Srinivas K said...

This was a very novel idea. May be useful to many.