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 (将#修改为@)