Page 685 - Introduction to Programming with Java: A Problem Solving Approach
P. 685

                By default, a program’s close-window button (the X in the top-right corner) doesn’t work very nicely. When the user clicks it, the window closes, but the program still runs in the background. To remedy this situation, call setDefaultCloseOperation(EXIT_ON_CLOSE). Then when the user clicks the close-window button, the window closes and the program terminates. Having a closed program run in the background is usually unnoticeable, and that’s why many programmers have a hard time remembering to call setDefaultCloseOperation(EXIT_ON_CLOSE). Nonetheless, you should try to remember to call it. If you forget to call it, and a user’s computer has limited memory and there are many programs run- ning in the background, the computer’s performance will degrade.
The add method adds a specified component to the current window. Once the component is added, it stays with the window for the life of the program. We mention this so that you’re comfortable using a local variable declaration for a component. In the following example, even though label is defined locally within createContents, the instantiated JLabel component stays with the window after createContents finishes:
private void createContents()
{
JLabel label = new JLabel("Hi! I'm Larry the label!");
add(label);
} // end createContents
Windows are invisible by default. To make a window and its contents visible, add the components to the window and then call setVisible(true). Do it in that order—add components first, then call setVisible. Otherwise, the added components won’t display. To make a window invisible, call setVisible(false).Apago PDF Enhancer
The JFrame class contains many additional methods, too many to mention here. If you’ve got some time on your hands, we encourage you to find out what’s available by looking up the JFrame class on Sun’s Java API Web site—http://java.sun.com/javase/6/docs/api/.
16.5 Java Components
Now let’s consider the objects that sit inside a window—the components. Here are some examples of Java
components:
• JLabel,JTextField,JButton,
• JTextArea,JCheckBox,JRadioButton,JComboBox • JMenuBar,JMenu,JMenuItem
These aren’t all of the Java components, just some of the more commonly used ones. We’ll describe the first three components in this chapter and the other components in the next chapter.
All of the above component classes are in the javax.swing package, so you must import that pack- age to use them. But remember that you’re already importing the javax.swing package to access the JFrame class. There’s no need to import it twice.
Component classes typically are derived from the JComponent class, which supports many useful inheritable features. Along with many other methods, the JComponent class contains methods that handle these component features:
• foregroundandbackgroundcolors • textfont
16.5 Java Components 651
 















































































   683   684   685   686   687