如何解决JButton 不会转到下一张卡片
我需要使用 JButton 在多个面板之间切换,第一个面板工作正常,但是按钮无法切换到其他面板。这是 Cardlayout 的主类和使用 cardLayout.next( );。我不确定接下来要做什么。
public class MainPanel{
/**
* @param args the command line arguments
*/
private static JFrame frame;
private static JButton button;
private static JPanel cards;
private static int panelChanger = 0;
public static void main(String args[]){
frame = new JFrame ("Draw");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
LinearPanel panel = new LinearPanel();
frame.getContentPane().add(panel);
CirclePanel panel2 = new CirclePanel();
frame.getContentPane().add(panel2);
AbsPanel panel3 = new AbsPanel();
frame.getContentPane().add(panel3);
JPanel cards = new JPanel(new CardLayout());
cards.add(panel,"LinearPanel");
cards.add(panel2,"CirclePanel");
cards.add(panel3,"AbsPanel");
button = new JButton("Next");
button.setForeground(Color.white);
button.setBounds(250,10,100,60);
button.addActionListener( new ButtonListener());
frame.add(button);
frame.getContentPane().add(cards);
frame.pack();
frame.setVisible(true);
int a = 0;
CardLayout cardLayout = (CardLayout) cards.getLayout();
}
static class ButtonListener implements ActionListener{
public void actionPerformed (ActionEvent event){
if(event.getSource() == button){
CardLayout cardLayout = (CardLayout) cards.getLayout();
cardLayout.next(cards);
frame.revalidate();
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。