我正在尝试使用卡片布局,并且我在顶部有 2 个按钮可以更改卡片,但由于某种原因它不起作用,下一个方法有效,但显示或第一个\最后一个不起作用,当然我不能使用下一个,因为我想要每个按钮都有一张特定的卡片,这是我的代码:
cards = new CardLayout();
cardPanel = new JPanel();
cardPanel.setLayout(cards);
cards.show(cardPanel, "gapas");
JPanel firstCard = new JPanel();
firstCard.setBackground(Color.WHITE);;
JPanel secondCard = new JPanel();
secondCard.setBackground(Color.blue);
cardPanel.add(firstCard, "kalam");
cardPanel.add(secondCard, "gapan");
guiFrame.add(tabsPanel,BorderLayout.NORTH);
guiFrame.add(cardPanel,BorderLayout.CENTER);
guiFrame.setVisible(true);
}
ActionListener action = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().matches("kalam")){
cards.show(cardPanel,"kalam");
System.out.println("kalam");
}
else{
cards.show(cardPanel, "gapas");
System.out.println("gapas");
}
}
};