CardLayout 代码示例

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.smartcardio.Card;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class CardFrame extends JFrame {
  private JPanel mainJp;
  private JPanel leftJp;
  private JPanel workJp;
  private JPanel studyJp;
  private JButton btnWork;
  private JButton btnStudy;
  private JButton btn1;
  private JButton btn2;
  private CardLayout card=new CardLayout();
  public CardFrame(){

   workJp=new JPanel();
   btn1=new JButton("工作界面");
   workJp.add(btn1);
  
   studyJp=new JPanel();
   btn2=new JButton("学习界面");
   studyJp.add(btn2);
  
   mainJp =new JPanel();
   mainJp.setLayout(card);
  
   btnWork=new JButton("工作");
   btnWork.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
   card.show(mainJp, "work");
    
   }
   
   });
   btnStudy=new JButton("学习");
   btnStudy.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    card.show(mainJp, "study");
    
   }
   
   });
  
  
   mainJp.setLayout(card);
   mainJp.add(workJp,"work");
   mainJp.add(studyJp,"study");
  
   this.add(mainJp,BorderLayout.CENTER);
  
   leftJp=new JPanel();
   leftJp.add(btnStudy);
   leftJp.add(btnWork);
   this.add(leftJp,BorderLayout.WEST);
   this.setVisible(true);
   this.setBounds(200, 200, 1000, 700);
  
  }
 public static void main(String [] args){
   CardFrame c=new  CardFrame();
 }
 
 
 

}

你可能感兴趣的:(Study,Summarize)