1 import javax.swing.*; 2 import java.awt.*; 3 import javax.swing.border.*; 4 public class TestSwingCommonFeatures extends JFrame 5 { 6 public TestSwingCommonFeatures() 7 { 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 Button"));//创建标题边界19 Font largeFont = new Font("TimesRoman",Font.BOLD,20);//创建字体对象20 Border lineBorder = new LineBorder(Color.BLACK,2);//创建一个线边界对象21 JPanel p2 = new JPanel(new GridLayout(1,2,5,5));//创建另一个面板,使用网格布局22 JLabel jlbRed = new JLabel("Red");23 JLabel jlbOrange = new JLabel("Orange");24 jlbRed.setForeground(Color.RED);//设计前景色25 jlbOrange.setForeground(Color.ORANGE);26 jlbRed.setFont(largeFont);//设计字体27 jlbOrange.setFont(largeFont);28 jlbRed.setBorder(lineBorder);//设计线边界29 jlbOrange.setBorder(lineBorder);30 p2.add(jlbRed);31 p2.add(jlbOrange);32 p2.setBorder(new TitledBorder("Two Labels"));//设置标题边界33 setLayout(new GridLayout(2,1,5,5));//边框内设置为网格布局34 add(p1);//将面板添加到边框内35 add(p2);36 }37 public static void main(String[] agrs)38 {39 JFrame frame = new TestSwingCommonFeatures();40 frame.setTitle("TestSwingCommonFeatures");41 frame.setSize(300,150);42 frame.setLocationRelativeTo(null);43 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);44 frame.setVisible(true);45 }46 }