Java Reference
In-Depth Information
24
25 // Create a panel to group two labels
26 JPanel p2 = new JPanel( new GridLayout( 1 , 2 , 5 , 5 ));
27 JLabel jlblRed = new JLabel( "Red" );
28 JLabel jlblOrange = new JLabel( "Orange" );
29 jlblRed.setForeground(Color.RED);
30
31
32 jlblOrange.setFont(largeFont);
33
34 jlblOrange.setBorder(lineBorder);
35 p2.add(jlblRed);
36 p2.add(jlblOrange);
37
38
39 // Add two panels to the frame
40 setLayout( new GridLayout( 2 , 1 , 5 , 5 ));
41 add(p1);
42 add(p2);
43 }
44
45 public static void main(String[] args) {
46 // Create a frame and set its properties
47 JFrame frame = new TestSwingCommonFeatures();
48 frame.setTitle( "TestSwingCommonFeatures" );
49 frame.setSize( 300 , 150 );
50 frame.setLocationRelativeTo( null ); // Center the frame
51 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
52 frame.setVisible( true );
53 }
54 }
set foreground
jlblOrange.setForeground(Color.ORANGE);
jlblRed.setFont(largeFont);
set font
jlblRed.setBorder(lineBorder);
set line border
p2.setBorder( new TitledBorder( "Two Labels" ));
set titled border
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 can invoke setVisible(false) . Please run the
program and see the effect after inserting the following two statements in line 38:
property default values
jbtLeft.setVisible( false );
jlblRed.setVisible( false );
12.21 How do you set background color, foreground color, font, and tool tip text on a Swing
GUI component?
12.22 Why is the tool tip text not displayed in the following code?
Check
Point
1
import javax.swing.*;
2
3
public class Test extends JFrame {
4
private JButton jbtOK = new JButton( "OK" );
5
6 public static void main(String[] args) {
7 // Create a frame and set its properties
8 JFrame frame = new Test();
9 frame.setTitle( "Logic Error" );
10 frame.setSize( 200 , 100 );
11 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
Search WWH ::




Custom Search