Java Programming
Total Page:16
File Type:pdf, Size:1020Kb
CHAPTER 15 Advanced GUI Topics In this chapter, you will: Use content panes Use color Learn more about layout managers Use JPanels to increase layout options Create JScrollPanes Understand events and event handling more thoroughly Use the AWTEvent class methods Handle mouse events Use menus Unless noted otherwise, all images are © 2014 Cengage Learning Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. CHAPTER 15 Advanced GUI Topics Understanding the Content Pane The JFrame class is a top-level container Java Swing class. (The other two top-level container classes are JDialog and JApplet.) Every GUI component that appears on the screen must be part of a containment hierarchy. A containment hierarchy is a tree of components that has a 802 top-level container as its root (that is, at its uppermost level). Every top-level container has a content pane that contains all the visible components in the container’s user interface. The content pane can directly contain components like JButtons, or it can hold other containers, like JPanels, that in turn contain such components. A top-level container can contain a menu bar. A menu bar is a horizontal strip that conventionally is placed at the top of a container and contains user options. The menu bar, if there is one, is just above (and separate from) the content pane. A glass pane resides above the content pane. Figure 15-1 shows the relationship between a JFrame and its root, content, and glass panes. JFrame Root pane Menu bar Content pane Glass pane Figure 15-1 Parts of a JFrame The glass pane is a powerful container feature. Tool tips, which you learned about in Chapter 14, reside on the glass pane. You also can draw your own graphics on the glass pane “on top of” components on a JFrame or JApplet. (You will learn about drawing in the Graphics chapter, and about JApplets in the chapter Applets, Images, and Sound.) If you add a MouseListener to the glass pane, it prevents the mouse from triggering events on the components below the glass pane on the JFrame. An additional layered pane exists above the root pane, but it is not often used explicitly by Java programmers. For more details, see the Java Web site. Whenever you create a JFrame (or other top-level container), you can get a reference to its content pane using the getContentPane() method. In Chapter 14, you added and removed components from JFrames and set their layout managers without understanding you were using the content pane. You had this ability because Java automatically converts add(), Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. Understanding the Content Pane remove(), and setLayoutManager() statements to more complete versions. For example, the following three statements are equivalent within a class that descends from JFrame: this.getContentPane().add(aButton); getContentPane().add(aButton); add(aButton); 803 In the first statement, this refers to the JFrame class in which the statement appears, and getContentPane() provides a reference to the content pane. In the second statement, the this reference is implied. In the third statement, both the this reference and the getContentPane() call are implied. Although you do not need to worry about the content pane if you only add components to, remove components from, or set the layout manager of a JFrame, you must refer to the content pane for all other actions, such as setting the background color. When you write an application that adds multiple components to a content pane, it is more efficient to declare an object that represents the content pane than to keep calling the getContentPane() method. For example, consider the following code in a JFrame class that adds three buttons: getContentPane().add(button1); getContentPane().add(button2); getContentPane().add(button3); You might prefer to write the following statements. The call to getContentPane() is made once, its reference is stored in a variable, and the reference name is used repeatedly with the call to the add() method: Container con = getContentPane(); con.add(button1); con.add(button2); con.add(button3); As an example, the class in Figure 15-2 creates a JFrame like the ones you created throughout Chapter 14, although to keep the example simple, the default close operation was not set and the button was not assigned any tasks. import java.awt.*; import javax.swing.*; public class JFrameWithExplicitContentPane extends JFrame { private final int SIZE = 180; private Container con = getContentPane(); private JButton button = new JButton("Press Me"); Figure 15-2 The JFrameWithExplicitContentPane class (continues) Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. CHAPTER 15 Advanced GUI Topics (continued) public JFrameWithExplicitContentPane() { super("Frame"); setSize(SIZE, SIZE); 804 con.setLayout(new FlowLayout()); con.add(button); } public static void main(String[] args) { JFrameWithExplicitContentPane frame = new JFrameWithExplicitContentPane(); frame.setVisible(true); } } Figure 15-2 The JFrameWithExplicitContentPane class In Figure 15-2, the getContentPane() method assigns a reference to a Container named con, and the Container reference is used later with the setLayout() and add() methods. Figure 15-3 shows the result. The frame constructed from the class in Figure 15-2 is identical to the one that would be constructed if the shaded parts were omitted. Figure 15-3 Output of the JFrameWithExplicitContentPane application When you want to use methods other than add(), remove(),orsetLayout(), you must use a content pane. In the next sections you will learn about methods such as setBackground(), which is used to change a JFrame’s background color, and setLayout(), which is used to change a JFrame’s layout manager. If you use these methods with a JFrame instead of its content pane, the user will not see the results. Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. Using Color TWO TRUTHS & ALIE Understanding the Content Pane 1. Every Java component has a content pane that contains all the visible parts a 805 user sees. 2. Whenever you create a JFrame, you can get a reference to its content pane using the getContentPane() method. 3. When you change the background color or layout of a JFrame, you should change the content pane and not the JFrame directly. srinterface. user s container the in components visible the all ’ a otn aeta contains that pane content a has Every #1. is statement false The o-ee container top-level Using Color The Color class defines colors for you to use in your applications. The Color class can be used with the setBackground() and setForeground() methods of the Component class to make your applications more attractive and interesting. When you use the Color class, you include the statement import java.awt.Color; at the top of your class file. The statement import java.awt.* uses the wildcard to import all of the types in the java.awt package, but it does not import java.awt.Color, java.awt.Font, or any other packages within awt. If you plan to use the classes from java.awt and from java.awt.Color, you must use both import statements. The Color class defines named constants that represent 13 colors, as shown in Table 15-1. Java constants are usually written in all uppercase letters, as you learned in Chapter 2. However, Java’s creators declared two constants for every color in the Color class—an uppercase version, such as BLUE, and a lowercase version, such as blue. Earlier versions of Java contained only the lowercase Color constants. (The uppercase Color constants use an underscore in DARK_GRAY and LIGHT_GRAY; the lowercase versions are a single word: darkgray and lightgray.) Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. CHAPTER 15 Advanced GUI Topics BLACK GREEN RED BLUE LIGHT_GRAY WHITE CYAN MAGENTA YELLOW 806 DARK_GRAY ORANGE GRAY PINK Table 15-1 Color class constants You can also create your own Color object with the following statement: Color someColor = new Color(r, g, b); In this statement, r, g, and b are numbers representing the intensities of red, green, and blue you want in your color.