6.1.3 一个简单的GUI程序
package six; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Main { private static int numclicks = 0; //记录次数 private static JFrame frame = new JFrame("简单GUI"); private static JLabel label = new JLabel("按钮单击次数:"+"0 "); private static JButton button = new JButton("点击我!~~~"); private static JPanel pane = new JPanel(); //容器 public static void main(String[] args) { pane.setBorder(BorderFactory.createEmptyBorder(60,60,20,60)); pane.setLayout(new GridLayout(0,1)); //设置布局 pane.add(button); pane.add(label); frame.getContentPane().add(pane,BorderLayout.CENTER); button.setMnemonic(KeyEvent.VK_I); button.addActionListener(new ActionListener(){ //鼠标单击事件 public void actionPerformed(ActionEvent e){ numclicks++; label.setText("按钮单击次数:"+numclicks); } }); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }
6.4.2 使用FlowLayout布局管理器
FlowLayout能够根据窗口的大小自动调整布局
package six; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Main { JButton b1,b2,b3,b4,b5; JFrame frame; //窗体 public void display(){ frame = new JFrame("测试书序布局管理器"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // b1 = new JButton("按钮 1"); b2 = new JButton("按钮 2"); b3 = new JButton("按钮 3"); b4 = new JButton("按钮 4"); b5 = new JButton("按钮 5"); FlowLayout flowLayout = new FlowLayout(); //顺序布局管理器 Container contentPane = frame.getContentPane(); //容器 contentPane.setLayout(flowLayout); contentPane.add(b1); contentPane.add(b2); contentPane.add(b3); contentPane.add(b4); contentPane.add(b5); contentPane.setSize(200,100); frame.pack(); frame.setVisible(true); } public static void main(String[]args) { new Main().display(); //傻B } }
6.4.3使用区域布局管理器BorderLayout
BorderLayout分东西南北中,如何缩放,其南北总是保持最佳高度。
package six; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Main { JButton b1,b2,b3,b4,b5; JFrame frame; //窗体 public void display(){ frame = new JFrame("测试书序布局管理器"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // b1 = new JButton("按钮 1"); b2 = new JButton("按钮 2"); b3 = new JButton("按钮 3"); b4 = new JButton("按钮 4"); b5 = new JButton("按钮 5"); BorderLayout borderLayout = new BorderLayout(); Container contentPane = frame.getContentPane(); //容器 contentPane.setLayout(borderLayout); contentPane.add(b1,BorderLayout.EAST); contentPane.add(b2,BorderLayout.WEST); contentPane.add(b3,BorderLayout.SOUTH); contentPane.add(b4,BorderLayout.NORTH); contentPane.add(b5,BorderLayout.CENTER); contentPane.setSize(200,100); frame.pack(); frame.setVisible(true); } public static void main(String[]args) { new Main().display(); //傻B } }
6.4.5卡片布局管理器CardLayout
卡片布局管理器把容器作为一个卡片,一次仅有一个卡片可见。
package six; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Main extends JFrame{ //类Main继承自JFrame private JPanel pane = null; private JPanel p = null; private CardLayout card = null; private JButton button_1 = null; private JButton button_2 = null; private JButton b1 = null,b2 = null,b3 = null; private JPanel p1 = null,p2 = null,p3 = null; public Main() // { super("卡片布局管理器测试"); try{ UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch(Exception ex){ ex.printStackTrace(); } //创建新卡片布局 card = new CardLayout(5,5); pane = new JPanel(card); p = new JPanel(); button_1 = new JButton("< 上一步"); button_2 = new JButton("下一步 >"); b1 = new JButton("1");b2 = new JButton("2");b3 = new JButton("3"); b1.setMargin(new Insets(2,2,2,2)); b2.setMargin(new Insets(2,2,2,2)); b3.setMargin(new Insets(2,2,2,2)); p.add(button_1);p.add(b1);p.add(b2);p.add(b3);p.add(button_2); p1 = new JPanel(); p2 = new JPanel(); p3 = new JPanel(); p1.setBackground(Color.RED); p2.setBackground(Color.BLUE); p3.setBackground(Color.GREEN); p1.add(new JLabel("JPanel_1")); p2.add(new JLabel("JPanel_2")); p3.add(new JLabel("JPanel_3")); pane.add(p1,"p1");pane.add(p2,"p2");pane.add(p3,"p3"); //翻转卡片布局动作 button_1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ card.previous(pane); } }); button_2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ card.next(pane); } }); b1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ card.show(pane, "p1"); } }); b2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ card.show(pane,"p2"); } }); b3.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ card.show(pane, "p3"); } }); this.getContentPane().add(pane); this.getContentPane().add(p,BorderLayout.SOUTH); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(300, 200); this.setVisible(true); } public static void main(String[]args) { new Main(); //傻B } }
时间: 2024-11-09 23:58:26