You have used several GUI components (e.g., JFrame , Container , JPanel , JButton , JLabel , JTextField ) in this chapter. Many more GUI components will be introduced in this book. It is important to understand the common features of Swing GUI components. The Component class is the root for all GUI components and containers. All Swing GUI components (except JFrame , JApplet , and JDialog ) are subclasses of JComponent , as shown in Figures 12.2 and 12.3. Figure 12.13 lists some frequently used methods in Component , Container , and JComponent for manipulating properties like font, color , size , tool tip text, and border.
A tool tip is a text displayed on a component when you move the mouse on the component. It is often used to describe the function of a component.
You can set a border on any object of the JComponent class. Swing has several types of borders. To create a titled border, use new TitledBorder(String title) . To create a line border, use new LineBorder(Color color, int width) , where width specifies the thickness of the line.
Listing 12.7 is an example to demonstrate Swing common features. The example creates a panel p1 to hold three buttons (line 8) and a panel p2 to hold two labels (line 25). The background of the button jbtLeft is set to white (line 12), and the foreground of the button jbtCenter is set to green (line 13). The tool tip of the button jbtRight is set in line 14. Titled borders are set on panels p1 and p2 (lines 18, 36), and line borders are set on the labels (lines 32 “33).
1 import java.awt.*; 2 import javax.swing.*; 3 import javax.swing.border.*; 4 5 public class TestSwingCommonFeatures extends JFrame { 6 public TestSwingCommonFeatures() { 7 // Create a panel to group three buttons 8 JPanel p1 = new JPanel( new FlowLayout(FlowLayout.LEFT, 2 , 2 )); 9 JButton jbtLeft = new JButton( "Left" ); 10 JButton jbtCenter = new JButton( "Center" ); 11 JButton jbtRight = new JButton( "Right" ); 12 jbtLeft.setBackground(Color.WHITE); 13 jbtCenter.setForeground(Color.GREEN); 14 jbtRight.setToolTipText( "This is the Right button" ); 15 p1.add(jbtLeft); 16 p1.add(jbtCenter); 17 p1.add(jbtRight); 18 p1.setBorder( new TitledBorder( "Three Buttons" )); 19 20 // Create a font and a line border 21 Font largeFont = new Font( "TimesRoman" , Font.BOLD, 20 ); 22 Border lineBorder = new LineBorder(Color.BLACK, 2 ); 23 24 // Create a panel to group two labels 25 JPanel p2 = new JPanel( new GridLayout( 1 , 2 , 5 , 5 )); 26 JLabel jlblRed = new JLabel( "Red" ); 27 JLabel jlblOrange = new JLabel( "Orange" ); 28 jlblRed.setForeground(Color.RED); 29 jlblOrange.setForeground(Color.ORANGE); 30 jlblRed.setFont(largeFont); 31 jlblOrange.setFont(largeFont); 32 jlblRed.setBorder(lineBorder); 33 jlblOrange.setBorder(lineBorder); 34 p2.add(jlblRed); 35 p2.add(jlblOrange); 36 p2.setBorder( new TitledBorder( "Two Labels" )); 37 38 // Add two panels to the frame 39 setLayout( new GridLayout( 2 , 1 , 5 , 5 )); 40 add(p1); 41 add(p2); 42 } 43 44 public static void main(String[] args) { 45 // Create a frame and set its properties 46 JFrame frame = new TestSwingCommonFeatures(); 47 frame.setTitle( "TestSwingCommonFeatures" ); 48 frame.setSize( 300 , 150 ); 49 frame.setLocationRelativeTo( null ); // Center the frame 50 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 51 frame.setVisible( true ); 52 } 53 } |
Note
The same property may have different default values in different components. For example, the visible property in JFrame is false by default, but it is true in every instance of JComponent (e.g., JButton and JLabel ) by default. To display a JFrame , you have to invoke setVisible(true) to set the visible property true , but you don't have to set this property for a JButton or a JLabel , because it is already true . To make a JButton or a JLabel invisible, you may invoke setVisible(false) on. Please run the program and see the effect after inserting the following two statements in line 37: jbtLeft.setVisible( false ); jlblRed.setVisible( false ); |