</pre><pre name="code" class="html">package chapter12; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridLayout; import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.border.LineBorder;//线框 import javax.swing.border.TitledBorder;//标题框 @SuppressWarnings("serial") public class TestSwingCommonFeatures extends JFrame{ public TestSwingCommonFeatures(){ JPanel p1 = new JPanel (new FlowLayout(FlowLayout.LEFT,2,2)); JButton jbtleft = new JButton("left"); JButton jbtcenter = new JButton("CENTER"); JButton jbtright = new JButton("right"); jbtleft.setBackground(Color.white); jbtcenter.setForeground(Color.green); jbtright.setToolTipText("this is the right button"); p1.add(jbtleft); p1.add(jbtcenter); p1.add(jbtright); p1.setBorder(new TitledBorder("three buttons")); //-------------------------------------// Font largeFont =new Font ("TimesRoman",Font.BOLD,20);//字体 JPanel p2 = new JPanel (new FlowLayout(FlowLayout.LEFT,5,5)); JButton jbtred = new JButton ("red"); JButton jbtorange = new JButton("orange"); jbtred.setFont(largeFont); jbtred.setBackground(Color.red); jbtorange.setForeground(Color.orange); jbtorange.setToolTipText("this is a orange button"); p2.add(jbtred); p2.add(jbtorange); p2.setBorder(new LineBorder(Color.green,4));//被下一行覆盖 p2.setBorder(new TitledBorder("a")); p2.setLayout(new GridLayout(1,2,1,10)); setLayout(new FlowLayout(FlowLayout.LEFT,30,30)); setLayout(new GridLayout(1,2,5,10));//下一行有一个新的setlayout 这一样的setlayout被覆盖,就不显示了 add(p1); add(p2); } public static void main(String[] args) { JFrame frame = new TestSwingCommonFeatures(); frame.setTitle("test swing common features"); frame.setSize(500, 150); frame.setLocationRelativeTo(null);//放中间 frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
展示
Font 字体
Color 颜色
Border 框
时间: 2024-11-06 09:38:40