Page 710 - Introduction to Programming with Java: A Problem Solving Approach
P. 710
676 Chapter 16 GUI Programming Basics
While the JFrame class handles perimeter features such as window dimensions, the title bar, and the close-out button, the content pane handles interior features such as components, layout, and background color. So when you add components, set the layout, and set the background color, you do it to the content pane, not the JFrame. These three statements illustrate what we’re talking about:
getContentPane().add(btn);
getContentPane().setLayout(new FlowLayout());
getContentPane().setBackground(Color.YELLOW);
In versions of Java prior to Java 5.0, JFrame’s getContentPane method was required for all three tasks—adding a component, setting the layout, and setting the window’s background color. With the advent of Java 5.0, the folks at Sun made things easier for the first two tasks. Now, if you want to add a component or set the layout, you may optionally omit the call to getContentPane. In other words, this works:
add(btn);
setLayout(new FlowLayout());
The reason that code works is that with the current version of Java, JFrame’s add and setLayout methods automatically get the content pane behind the scenes. And the retrieved content pane is used for the ensuing add and setLayout operations. So which is better—add(btn) or getContentPane().add(btn)? They are functionally equivalent, but the first one is generally preferred since it’s less cluttered. Ditto for the setLayout method call.
For setting the window’s background color, the current version of Java still requires that you call
getContentPane before calling setBackground. If you call setBackground without calling
getContentPane, it sets the JFrame’s background color, not the content pane’s background color. And Apago PDF Enhancer
since the content pane sits on top of the JFrame, the JFrame’s color is covered up and not seen.
Now you know that setting a window’s background color requires getContentPane. Similarly, get-
ting a window’s background color requires getContentPane. For example: Color saveColor = getContentPane().getBackground();
ColorChooser Program
Let’s put what you’ve learned about color into practice by using it within a complete program. In our Color- Chooser program, we implement light gray and light blue buttons that set the window’s background color to gray or blue, respectively. See Figure 16.13 to get an idea of what we’re talking about.
See the ColorChooser program listing in Figures 16.14a and 16.14b. Most of the code should already make sense since its structure mirrors the structure in our previous GUI programs. We’ll focus on the new code—the color code.
Note the difference between the setBackground calls in the createContents method and the setBackground calls in the actionPerformed method. In createContents, we’re dealing with the gray and blue button components, so it is not necessary to call getContentPane prior to calling setBackground. In actionPerformed, we’re dealing with the JFrame window, so it is necessary to call getContentPane prior to calling setBackground.
Note the following line from the createContents method. It sets the blue button color to light blue:
blueButton.setBackground(new Color(135, 206, 250));
There is no named constant for light blue so we have to instantiate a light-blue Color object by using an RGB value. We use almost the maximum amount of blue (250) as well as a substantial amount of red (135) and green (206). Are you curious why we’re using so much red and green? To achieve a light shade, you