如何从卡上的按钮切换卡?

时间:2017-01-25 12:16:02

标签: java jframe jpanel

我一直在寻找解决方案一段时间,尽管有类似的问题和答案,但发现似乎没有任何效果。我希望用户能够在卡片布局中设置各种面板。但是,我希望用于在这些卡之间切换的按钮位于卡本身上,而不是一组单独的按钮,这些按钮不会在程序中发生变化。这是创建框架的主文件:

public class BattleGraphs_V1 
{
    JPanel cards;

    public int cardNumber = 1;

    public void addComponentToPane (Container pane)
    {
        JPanel MainMenuCard = new MainMenu_V1();
        JPanel DifficultySelectorCard = new DifficultySelector_V1();

        cards = new JPanel(new CardLayout());

        cards.add(MainMenuCard);
        cards.add(DifficultySelectorCard);

        pane.add(cards, BorderLayout.CENTER);
    }

    private static void createAndShowGUI() 
    {
        JFrame frame = new JFrame("BattleGraphs");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        frame.setPreferredSize(new java.awt.Dimension(725, 420));
        frame.setResizable(false);

        BattleGraphs_V1 containerPanel = new BattleGraphs_V1();
        containerPanel.addComponentToPane(frame.getContentPane());

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) 
    {
        javax.swing.SwingUtilities.invokeLater(new Runnable() 
        {
            public void run() 
            {
                createAndShowGUI();
            }
        });
    }
}

这两个面板(将来会有更多)在项目中有两个单独的文件。除了每个面板上单个按钮的动作监听器之外,我还没有为它们添加代码,它是所有自动生成的代码。

编辑: 我向MainMenu JPanel的按钮添加了一个自动生成的动作侦听器,如下所示:

private void MainMenuCardSBActionPerformed(java.awt.event.ActionEvent evt) {                                               
        BattleGraphs_V1 BG_V1 = new BattleGraphs_V1();
        CardLayout cardLayout = (CardLayout) (BG_V1.cards.getLayout());
        cardLayout.show(BG_V1.cards, "DifficultyCard");
    }

在主文件(主文件为BattleGraphs_V1)中添加的名称添加后,如下所示:

cards.add(MainMenuCard, "MainMenuCard");
cards.add(DifficultySelectorCard, "DifficultyCard");

当我运行程序并单击该按钮时,得到了一个空指针异常错误,正如我已经完成了类似的解决方案。假设这更多地归结为范围问题,我会错吗?

第二次编辑: 是的,希望这应该有所帮助,我已将enite项目添加到GitHub存储库 - https://github.com/charga600/BattleGraphs/tree/master

1 个答案:

答案 0 :(得分:0)

当您将卡片添加到面板时,您应该将它们命名为,以便您可以选择要在特定事件中显​​示的开关。

cards = new JPanel(new CardLayout());

cards.add(MainMenuCard, "first");
cards.add(DifficultySelectorCard, "second");

pane.add(cards, BorderLayout.CENTER);

GUI:

JButton button = new JButton("Click here");
button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        CardLayout cl = (CardLayout) (cards.getLayout());
        cl.show(cards, "second");
    }
});

修改

这是演示CardLayout的简短示例,因为您没有共享完整的代码。

主要课程

    public class MainMenu_V1 extends JFrame {

    private JMenuBar menuBar;
    private JMenu file;
    private JMenuItem exit;
    private JPanel mainPanel;

    public MainMenu_V1() {
        setTitle("Main Panel");
        setResizable(false);
        setSize(new Dimension(400, 200));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setJMenuBar(createMainMenu());
        setLocationRelativeTo(null);

        mainPanel = new JPanel();
        mainPanel.setLayout(new CardLayout());
        mainPanel.add(new FirstPanel(mainPanel), "FIRST");
        mainPanel.add(new SecondPanel(mainPanel), "SECOND");

        setContentPane(mainPanel);
    }

    public JMenuBar createMainMenu() {
        menuBar = new JMenuBar();
        file = new JMenu("File");
        exit = new JMenuItem("Exit");
        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        file.add(exit);
        menuBar.add(file);

        return menuBar;
    }

    public void switchPanel(Container container, String panelName) {
        CardLayout card = (CardLayout) (container.getLayout());
        card.show(container, panelName);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new MainMenu_V1().setVisible(true);
            }
        });
    }
}

第一小组

public class FirstPanel extends JPanel {

    private JButton button;
    private JPanel mainPanel;

    public FirstPanel(JPanel mainPanel) {
        this.mainPanel = mainPanel;
        setPreferredSize(new Dimension(400, 200));
        setBackground(Color.GRAY);
        add(createButton());        
    }   

    private JButton createButton() {
        button = new JButton("Switch to the second Panel");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                MainMenu_V1 main = new MainMenu_V1();
                main.switchPanel(mainPanel, "SECOND");                
            }
        });
        return button;
    }
}

第二个小组

public class SecondPanel extends JPanel {

    private JButton button;
    private JPanel mainPanel;

    public SecondPanel(JPanel mainPanel) {
        this.mainPanel = mainPanel;
        setPreferredSize(new Dimension(400, 200));
        setBackground(Color.ORANGE);
        add(createButton());
    }   

    private JButton createButton() {
        button = new JButton("Switch to the first Panel");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                MainMenu_V1 main = new MainMenu_V1();
                main.switchPanel(mainPanel, "FIRST");                
            }
        });
        return button;
    }
}