如何解决您如何在 Java CardLayout 中选择特定卡片?
我在 JFrame 上使用卡片布局作为刷新元素,并希望在用户选择按钮后切换到特定卡片。有没有办法在动作事件上调用 CardLayout 中的特定卡片?
public class ComboBoxTest() extends JFrame implements ActionListener {
JPanel comboBoxPane = new JPanel(); //use FlowLayout
JButton cb = new JButton("next");
cb.setActionCommand(next);
cb.addActionListener(this);
cb.setText("Forward");
this.add(cb);
comboBoxPane.add(cb);
JButton cb2 = new JButton("next");
cb2.setActionCommand(previous);
cb2.addActionListener(this);
cb2.setText("Back");
this.add(cb2);
comboBoxPane.add(cb2);
//Create the "cards".
card1 = new JPanel();
card1.add(HomeGui.getBody());
card2 = new JPanel();
card2.add(HomeGui.getSecondCard());
card3 = new JPanel();
card3.add(Start.getbody());
//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1,next);
cards.add(card2,previous);
Container base = getContentPane();
pane.add(header,BorderLayout.BEFORE_FIRST_LINE);
pane.add(comboBoxPane,BorderLayout.AFTER_LAST_LINE);
pane.add(cards);
}
public void actionPerformed(ActionEvent e) {
CardLayout c1 = (CardLayout)(cards.getLayout());
String button = e.getActionCommand();
if(button.equals(next)) {
c1.last(cards);
}
if(button.equals(previous)) {
c1.first(cards);
}
if(button.equals(card3)) {
//calls the third,specified card
}
}
}
解决方法
必须使用 CardLayout 对象的 show 方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。