Swing 中卡片布局管理的简单使用

来源:互联网 发布:网络预警信息包括 编辑:程序博客网 时间:2024/04/30 02:49
import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class CardLay extends JFrame implements ActionListener{CardLayout cl = null; JPanel jp1,jp2,jp3,jp4,jp5;JButton jb1,jb2,jb3;public static void main(String[] args) {CardLay a1 =new CardLay();}public CardLay(){cl = new CardLayout();jp1 = new JPanel(cl);jp2 = new JPanel();jp2.add(new JLabel("a"));jp3 = new JPanel();jp3.add(new JLabel("b"));jp4 = new JPanel();jp4.add(new JLabel("c"));jp1.add(jp2, "0");//"0"相当于是给这个Jpanel起了一个名字。下面调用的时候用jp1.add(jp3, "1");jp1.add(jp4, "2");jp5 = new JPanel();jb1 = new JButton("上一张");jb1.addActionListener(this);jb2 = new JButton("中间");jb2.addActionListener(this);jb3 = new JButton("下一张");jb3.addActionListener(this);jp5.add(jb1);jp5.add(jb2);jp5.add(jb3);this.add(jp5, BorderLayout.SOUTH);this.add(jp1);this.setSize(400, 300);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif(e.getSource()==jb1){cl.show(jp1, "0");}else if(e.getSource()==jb2){cl.show(jp1, "1");}else if(e.getSource()==jb3){cl.show(jp1, "2");}}}

原创粉丝点击