现在可以运行起来,不过知道是否满足你的要求~~
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class useCardLayout extends JFrame {
public CardLayout mycard1 = new CardLayout(2,3);//2,3表示与边缘的距离
public JButton bt1= new JButton("key1");
public JButton bt2 = new JButton("key2");
public JButton bt3 = new JButton("key3");
public Container c = this.getContentPane();
useCardLayout(){
super("卡片管理");
setSize(400,300);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
this.add(bt1);
this.add(bt2);
this.add(bt3);
this.setLayout(mycard1); //应该是这样设置Layout吧~
/* c.add("mycard1",bt1);
c.add("mycard1",bt2);
c.add("mycard",bt3);*/
Lis list = new Lis();
bt1.addActionListener(list);
bt2.addActionListener(list);
bt3.addActionListener(list);
setResizable(true);
setVisible(true);
}
class Lis implements ActionListener {
//public static void main(String args[]){ } //我想知道为什么这里会有一个main方法?
//我觉得除非这个是静态内部类,否则
//不应该有静态方法~
public void actionPerformed(ActionEvent e){
Object r=e.getSource();
if(r==bt1 || r==bt2) mycard1.next(c);
else if(r==bt3) mycard1.first(c);
}
}
public static void main(String args[]){
new useCardLayout();
}
}