Java Reference
In-Depth Information
To change this, you must call a frame's setDefaultCloseOperation() method with one
of four static variables of the JFrame class as an argument:
EXIT_ON_CLOSE —Exit the application when the frame is closed.
n
DISPOSE_ON_CLOSE —Close the frame, remove the frame object from memory, and
keep running the application.
n
DO_NOTHING_ON_CLOSE —Keep the frame open and continue running.
n
HIDE_ON_CLOSE —Close the frame and continue running.
n
9
To prevent a user from closing a frame at all, add the following statement to the frame's
constructor method:
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
If you are creating a frame to serve as an application's main user interface, the expected
behavior is probably EXIT_ON_CLOSE , which shuts down the application along with the
frame.
Developing a Framework
Listing 9.1 contains a simple application that displays a frame 300
100 pixels in size.
This class can serve as a framework—pun unavoidable—for any applications you create
that use a graphical user interface.
×
LISTING 9.1
The Full Text of SimpleFrame.java
1: import javax.swing.JFrame;
2:
3: public class SimpleFrame extends JFrame {
4: public SimpleFrame() {
5: super(“Frame Title”);
6: setSize(300, 100);
7: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
8: setVisible(true);
9: }
10:
11: public static void main(String[] arguments) {
12: SimpleFrame sf = new SimpleFrame();
13: }
14: }
When you compile and run the application, you should see the frame displayed in
Figure 9.1.
 
Search WWH ::




Custom Search