In order to set up and run a Swing graphical user interface program, you need to do the following.
An applet is displayed in a browser, which uses your HTML file to accomplish most of the steps above.
init() message.
It assumes that you have set up the components, their layout, and their
behavior in the init() method or its helper methods.
show() message.
A stand-alone application is displayed using a Java virtual machine, which
is started up when you give the java command.
The virtual machine then executes the main() method in the
class specified in the command line.
The instructions in this section assume that you are writing a program that
is used only as a stand-alone application.
If you want to use a program as both an applet and a stand-alone
application, see the following section.
main() method should create a JFrame object or an object
from a JFrame subclass.
main() method is usually implemented in the same class.
setSize() method and the position is
set with the setPosition() method.
Both of these methods have two int parameters.
The parameters for setSize() specify the width and height in
pixels.
The parameters for setPosition() specify the x and y
coordinates of the upper left corner of the frame in pixels.
show() method.
It has no parameters.
If you want to run a program either as an applet or as a stand-alone application, you can begin by writing it just as an applet, then modifying it to run also as a stand-alone application.
The simplest way to modify an applet to run as a stand-alone application is to add a main() method to the JApplet subclass that defines the applet. The main() method should do the following.
The following template shows the code that is needed. Just substitute the data that you want for the italicized portions of the code.
public static void main(String[] args) {
// Create an instance of the applet class.
JApplet applet = new AppletClass();
// Send the applet an init() message.
applet.init();
// Construct a JFrame.
final JFrame frame =
new JFrame("FrameTitle");
// Transfer the applet's context pane to the JFrame.
frame.setContentPane(applet.getContentPane());
// Transfer the applet's menu bar into the JFrame.
// This line can be omitted if the applet
// does not create a menu bar.
frame.setJMenuBar(applet.getJMenuBar());
// Add a window listener to the frame for shutting
// down the application.
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.dispose();
System.exit(0);
}
});
// Set the size of the frame.
// To pack the frame as tightly as possible
// replace the setSize() message with the following.
// frame.pack();
frame.setSize(FrameWidth, FrameHeight);
// Set the location of the frame.
frame.setLocation(FrameX, FrameY);
// Show the frame.
frame.show();
// Invoke the applet's start() method.
// This line can be omitted if the applet
// does not define a start method.
applet.start();
}
After this code has been added to the class, you should recompile the class and execute it with the following command.
java AppletClass