main method. This template
is a reasonable way to start an application, where
GenericGUI is a subclass of JPanel
which contains the code to build a GUI with the appropriate
listeners.
This code is typical for building a window -- create a JFrame, tell what to do when the close box is clicked, put a panel with content in the window, arrange (pack) the components according to their layout, and finally make the window visible and start the GUI monitoring process (show).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// GenericApp.java - This is the main method for a generic application.
// Fred Swartz, 2003-Apr
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/////////////////////////////////////////////////////// class GenericApp
class GenericApp {
//====================================================== method main
public static void main(String[] args) {
JFrame window = new JFrame("Generic Application");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setContentPane(new GenericGUI());
window.pack();
window.show();
}//end main
}//endclass GenericApp
|
public static void main(String[] args)
The main method can be in any class, but
putting it in a class by itself makes the program simpler
to understand, at least at first.
new GenericGUI() and sets the
content pane to this new JPanel. Of course, you must
replace "GenericGUI" with the name of your GUI class.
pack() call arranges
the components in the layout. After this call,
the position and size of all components is known,
and hence the size of the window.
show(), or the equivalent
setVisible(true), makes the window
visible.
However, it does something that isn't so obvious.
It starts up another thread to monitor
the GUI interface -- watching for mouse clicks
etc. This thread is part of the
Java runtime system and coninues to run until
your program quits.
After intialization, everything is done only when a listener is called. The program sleeps until the user does something.