How to Use the JFrame Class in Java
What is a JFrame? Like any other Java class, a JFrame has several parts. Like the String, each object formed from the JFrame class has both data parts and method parts. The data parts include the frame’s height and width. The method parts include add, setDefaultCloseOperation, pack, and setVisible. All told, the JFrame class has about 320 methods.
You can’t use dots to refer to a frame’s height or width. But you can call many JFrame methods with those infamous dots. You call the frame’s methods by writing add(label), frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE), frame.pack(), and frame.setVisible(true).
Here’s the scoop on the JFrame methods:
The call frame.add(label) plops the label onto the frame. The label displays my androidBook.jpg picture, so this call makes the picture appear on the frame.
A call to frame.setDefaultCloseOperation tells Java what to do when you try to close the frame. For a frame that’s part of a larger application, you may want the frame to disappear when you click the x, but you probably don’t want the application to stop running.
The frame is the entire application — the whole enchilada. So when you click the x, you want the Java Virtual Machine to shut itself down. To make this happen, you call the setDefaultCloseOperation method with parameter JFrame.EXIT_ON_CLOSE. The other alternatives are as follows:
JFrame.HIDE_ON_CLOSE: The frame disappears, but it still exists in the computer’s memory.
JFrame.DISPOSE_ON_CLOSE: The frame disappears and no longer exists in the computer’s memory.
JFrame.DO_NOTHING_ON_CLOSE: The frame still appears, still exists, and still does everything it did before you clicked the x. Nothing happens when you click x. So with this DO_NOTHING_ON_CLOSE option, you can become very confused.
If you don’t call setDefaultCloseOperation, Java automatically chooses the HIDE_ON_CLOSE option. When you click the x, the frame disappears, but the Java program keeps running. Of course, with no visible frame, the code doesn’t do much. The only noticeable effect of the run is your development environment’s behavior.
With Eclipse, the little square in the Console view's toolbar retains its bright red color. When you hover over the square, you see the Terminate tooltip. So to end the Java program’s run, simply click this little square.
A frame’s pack method shrink-wraps the frame around whatever has been added to the frame. Without calling pack, the frame can be much bigger or much smaller than is necessary.
Unfortunately, the default is to make a frame much smaller than necessary. If you forget to call frame.pack, you get the tiny frame. Sure, you can enlarge the frame by dragging the frame’s edges with your mouse. But why should you have to do that? Just call frame.pack, instead.
Calling setVisible(true) makes the frame appear on your screen. If you forget to call setVisible(true, when you run the, you’ll see nothing on your screen. It’s always so disconcerting until you figure out what you did wrong.