CardLayout卡片式布局

package com.swing.layerout;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class CardTest extends JFrame{
    
    private CardLayout card;
    private JPanel jp; //用于显示card
    private JButton jb1;//用于切换card的显示
    private JButton jb2;//用于切换card的显示
    

    public CardTest(){
        
        init();
        
        
    }
    
    public void init(){
        
        this.setSize(400,300);
        this.setTitle("第一个JFrame窗体");
        //this.setIconImage(image);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//这里是简单的处理了用户点击关闭的事件,
                                                            //如果想复杂的处理话就要使用事件处理机制
        
        JPanel j1 = new JPanel();//这是第一个面板
        j1.add(new JLabel("这是第一个面板显示的内容"));
        
        JPanel j2 = new JPanel();//这是第一个面板
        j2.add(new JLabel("这是第二个面板显示的内容"));
        
        JPanel j3 = new JPanel();//这是第一个面板
        j3.add(new JLabel("这是第三个面板显示的内容"));
        
        card = new CardLayout();
        jp = new JPanel();
        jp.setLayout(card);
        //将三个面板加入到带card的jp中
        jp.add(j1,"j1");
        jp.add(j2,"j2");
        jp.add(j3,"j3");
        
        //切换jp中面板按钮jb1
        jb1 = new JButton("向前切换");
        jb1.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                card.previous(jp);    
            }
        });
        jb2 = new JButton("向后切换");
        jb2.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                card.next(jp);    
            }
        });
        JPanel jpSouth = new JPanel();
        jpSouth.setLayout(new GridLayout(1,2));
        jpSouth.add(jb1);jpSouth.add(jb2);
        //设置窗体布局为BorderLayout
        this.setLayout(new BorderLayout());
        
        this.add(jp,BorderLayout.CENTER);
        this.add(jpSouth,BorderLayout.SOUTH);
        
        this.setVisible(true);
        
        
    }
    
    
    public static void main(String[] args) {
        
        new CardTest();
    }
    
    

    public CardLayout getCard() {
        return card;
    }

    public void setCard(CardLayout card) {
        this.card = card;
    }

    public JPanel getJp() {
        return jp;
    }

    public void setJp(JPanel jp) {
        this.jp = jp;
    }

    public JButton getJb1() {
        return jb1;
    }

    public void setJb1(JButton jb1) {
        this.jb1 = jb1;
    }

    public JButton getJb2() {
        return jb2;
    }

    public void setJb2(JButton jb2) {
        this.jb2 = jb2;
    }

}

 

posted @ 2012-10-20 09:52  邹晟  阅读(2987)  评论(0编辑  收藏  举报