在JPanel 里面放了3個按鈕,默認的是顯示最前面添加的那個按鈕,可以是我想他顯示添加的第二個按鈕。怎么使用show來切換到第二個按鈕,讓它顯示出來呢?
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());
}
}
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());
}
}