In order to set up and run a Swing graphical user interface program, you need to do the following.
How this is done depends on the type of program:
An applet is displayed in a browser, which uses your HTML file to meet most of the requirements.
init()
message.
It assumes that you have set up the components, their layout, and
their behavior in the init()
method or its helper
methods.
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 or the
pack()
method.
The setSize()
method has two int
parameters,
specifying the width and height in pixels.
The pack()
method has no parameters.
The position is set with the setPosition()
method.
The setPosition()
method has two int
parameters specifying the x and y coordinates of the upper left corner
of the frame in pixels.
setVisible(true)
message.
In addition, it is desirable to have the application respond to a user click on the close button. This can be accomplished by sending the following message to the frame.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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.
init()
message.
start()
method then invoke it.
A template shows the organization of the main()
method
code.
The following template shows the code that is needed to combine applets and stand-alone applications. It should be added to the applet class. Just substitute the data that you want for the blue 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()); // Make the application shut down when the user clicks // on the close button. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 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.setVisible(true); // Invoke the applet's start() method. // This line can be omitted if the applet // does not define a start method. applet.start(); }