我在JPanel 里面放了3个按钮,默认的是显示最前面添加的那个按钮,可以是我想他显示添加的第二个按钮。怎么使用show来切换到第二个按钮,让它显示出来呢?public class Text extends JFrame
{
public Text()
{
this.setSize(400,350);
this.setLocation(200,100);

Container c = this.getContentPane();
c.setLayout(new GridLayout(2,1));

JPanel jp1 = new JPanel();
jp1.setLayout(new CardLayout());
jp1.setBorder(BorderFactory.createTitledBorder("注意这里"));

JButton jb1 = new JButton("按钮1");
jb1.setMargin(new Insets(0,0,0,0));
jp1.add(jb1,"aa");

JButton jb2 = new JButton("按钮2");
jb2.setMargin(new Insets(0,0,0,0));
jp1.add(jb2,"bb");

JButton jb3 = new JButton("按钮3");
jb3.setMargin(new Insets(0,0,0,0));
jp1.add(jb3,"cc");

c.add(jp1);
}
public static void main(String[] args)
{
new Text().setVisible(true);
}
}

解决方案 »

  1.   

     我是这样写的,不知道错在那里了? jp1.show(c,aa);/jp1.show(c,"aa");
      

  2.   


    public class Text extends JFrame implements ActionListener{
    public Text() {
    this.setSize(400, 350);
    this.setLocation(200, 100); Container c = this.getContentPane();
    c.setLayout(new GridLayout(3, 3)); JPanel jp1 = new JPanel();
    jp1.setLayout(new CardLayout(3,1));
    jp1.setBorder(BorderFactory.createTitledBorder("注意这里")); JButton jb1 = new JButton("按钮1");
    jb1.setMargin(new Insets(0, 0, 0, 0));
    jp1.add(jb1, "aa");
    jb1.addActionListener(this); JButton jb2 = new JButton("按钮2");
    jb2.setMargin(new Insets(0, 0, 0, 0));
    jp1.add(jb2, "bb");
    jb2.addActionListener(this); JButton jb3 = new JButton("按钮3");
    jb3.setMargin(new Insets(0, 0, 0, 0));
    jp1.add(jb3, "cc");
    jb3.addActionListener(this);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    c.add(jp1);
    } public static void main(String[] args) {
    new Text().setVisible(true);
    } public void actionPerformed(ActionEvent e) {
    JButton btn = (JButton) e.getSource();
    CardLayout card = (CardLayout) btn.getParent().getLayout();
    card.next(btn.getParent());
    }
    }