java卡片布局管理实例
卡片布局实例如下:
package yjh.gui; import java.awt.CardLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class CardLayoutDemo extends JFrame implements ActionListener { JButton jb1, jb2, jb3; JPanel jp1, jp2, jp3, jp4, jp5; CardLayout c1; public CardLayoutDemo() { jb1 = new JButton("卡片1"); jb1.addActionListener(this); jb2 = new JButton("卡片2"); jb2.addActionListener(this); jb3 = new JButton("卡片3"); jb3.addActionListener(this); c1 = new CardLayout(); jp1 = new JPanel(c1); // 创建三个JPanel jp3 = new JPanel(); jp3.add(new JLabel("卡片1")); jp4 = new JPanel(); jp4.add(new JLabel("卡片2")); jp5 = new JPanel(); jp5.add(new JLabel("卡片3")); // 把3个面板加入到卡片jp1中,默认显示第一个加入的卡片 jp1.add(jp3, "0"); jp1.add(jp4, "1"); jp1.add(jp5, "2"); jp2 = new JPanel(); jp2.add(jb1); jp2.add(jb2); jp2.add(jb3); this.add(jp1, "Center"); this.add(jp2, "South"); this.setSize(400, 300); this.setTitle("卡片布局实例"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocation(300, 300); this.setVisible(true); } public static void main(String[] args) { new CardLayoutDemo(); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == jb1) { c1.show(jp1, "0"); } else if (e.getSource() == jb2) { c1.show(jp1, "1"); } else if (e.getSource() == jb3) { c1.show(jp1, "2"); } } }

浙公网安备 33010602011771号