Page 683 - Introduction to Programming with Java: A Problem Solving Approach
P. 683
To use the pre-built GUI classes, you’ll need to import them into your GUI programs. You could import the classes individually, but there’s a better way. Recall that a package is a collection of pre-built classes. Since most of the critical pre-built GUI classes come from the javax.swing and java.awt packages, import those two packages and you’ll import most of the critical pre-built GUI classes. Get used to importing those two packages in every one of your GUI programs. Recall that to import a package, you need to use an as-
terisk; that is, import within a particular package.
javax.swing.*;. The * is a wildcard, and it allows you to import all the classes
InSimpleWindow’sclassheading,notetheextends JFrameclause.TheJFrameclassispart of the GUI framework mentioned above. The JFrame class provides standard Windows features such as a title bar, a minimize button, and so on. Below the class heading, note the WIDTH and HEIGHT named con- stants. They’re used by the setSize method call to specify the dimensions of the window.
Let’s now examine the main method. GUI programs typically create a window with GUI components, and then they just sit around waiting for the user to do something like click a button, select a menu option, and so on. Thus, main is very short—it just instantiates the window and that’s it. In this simple example, we don’t even bother to assign the instantiated window object to a reference variable. Review: What do you call an object that isn’t stored in a reference variable? An anonymous object.
In performing the anonymous-object instantiation, main calls the SimpleWindow constructor. The SimpleWindow constructor (1) calls setTitle to assign the window’s title, (2) calls setSize to assign the window’s size, (3) calls setLayout to assign the window’s layout scheme, and (4) calls setDefaultCloseOperation to enable the close-window button (the “X” in the top-right corner) to work properly.
In the interest of modularization, the SimpleWindow constructor then calls a helper method, createContents, to crAeapte tahegcoomponPenDts tFhat gEo insihdeathenwcindeowr. The createContents method contains only two lines. With only two lines, there’s really no need for a helper method, but we want you to form good habits. For this trivial example, there’s only one component and there’s no event handler for the component. Thus, two lines are all that’s needed. But normal GUI programs have multiple components and multiple event handlers. For that, quite a few lines are needed. If you stick those lines in the constructor, you’d have a long constructor. Better to break things up and stick them in a helper method.
The createContents method instantiates a JLabel component and then calls the add method to add the JLabel component to the window. A JLabel component is the simplest type of GUI component. It’s a piece of text that the user can read but cannot change.
After executing createContents, the JVM returns to the SimpleWindow constructor. The SimpleWindow constructor then calls setVisible to make the window visible.
16.4 JFrame Class
In the previous section, we introduced you to the JFrame class. In this section, we describe the JFrame
class in more depth. More specifically, we cover its characteristics and its methods.
JFrame Basics
These days, most purchasable software is windows-based. When you load such software, you’ll see a win- dow and that window will have a title bar, a border, a minimize button, a close-window button, the ability to resize the window, and so on. You could implement all those features from scratch in your own classes, but why “reinvent the wheel”? The JFrame class implements the standard windows features that you’ve come to know and love. To get all that cool windows functionality for free, just implement your classes by extend- ing the JFrame class. What a deal!
16.4 JFrame Class 649