如何在我的主游戏屏幕上添加纸牌布局格式,以便在单击按钮时将其从主菜单屏幕更改为我的游戏屏幕代码

鹰97

我正在为一周内要完成的A2计算课程编写跳棋游戏。我已经完全完成了游戏,剩下要做的就是通过我制作的CardLayout将两个JPanels连接在一起。但是我不确定该怎么做,如果有人能告诉我我将非常感激,我已经尝试了好一阵子了,这只是在弄乱我的结构。谢谢

这是我的CardLayout中的代码:

public class CLayout extends JPanel implements ItemListener {
private JFrame ourFrame = new JFrame("Main Menu");
private JPanel ourMasterPanel, comboxPanel, cardPanel, cardPanelOne, cardPanelTwo;
private String[] boxitems = { "Play Checkers", "Options"};
private JComboBox<String> ourBox = new JComboBox<String>(boxitems);


private JButton[] ourButtons = new JButton[]
        {
        new JButton("Single Player"),
        new JButton("Multiplayer"),
        new JButton("Rules for Checkers"),
        new JButton("Fun Facts about Checkers"),
        new JButton("Checkers Strategy and Tips"),
        new JButton("Exit")
        };

CLayout()
{
    ourMasterPanel = (JPanel) ourFrame.getContentPane();
    ourMasterPanel.setLayout(new BorderLayout());

    comboxPanel = new JPanel();
    comboxPanel.setLayout(new FlowLayout());
    comboxPanel.add(ourBox);

    ourBox.addItemListener(this);
    ourBox.setEditable(false);

    cardPanel = new JPanel();
    cardPanel.setLayout(new CardLayout());

    cardPanelOne = new JPanel();
    cardPanelOne.add(ourButtons[0]);
    cardPanelOne.add(ourButtons[1]);

    cardPanelTwo = new JPanel();
    cardPanelTwo.add(ourButtons[2]);
    cardPanelTwo.add(ourButtons[3]);
    cardPanelTwo.add(ourButtons[4]);
    cardPanelTwo.add(ourButtons[5]);


    cardPanel.add(cardPanelOne,boxitems[0]);
    cardPanel.add(cardPanelTwo,boxitems[1]);

    ourMasterPanel.add(comboxPanel,BorderLayout.NORTH);
    ourMasterPanel.add(cardPanel,BorderLayout.SOUTH);

    ourFrame.setVisible(true);
    ourFrame.setSize(350,250);
    ourFrame.setResizable(false);
    ourFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    ourFrame.pack();
    ourFrame.validate();

}

@Override
public void itemStateChanged(ItemEvent e) {
    // TODO Auto-generated method stub

    CardLayout cardLayout = (CardLayout)(cardPanel.getLayout());
    cardLayout.show(cardPanel, (String)e.getItem());

}
}

我将显示的代码保持在最低限度,因此我删除了按钮的所有动作侦听器,无论如何,我有一个问题是,我希望这样,以便用户单击“多人”按钮时数组按钮ourButtons [1],它将切换到我的主游戏屏幕,以便用户随后可以玩跳棋游戏。

这是我的CheckerBoard类的主要重要GUI:

public class CheckerBoard extends JPanel implements ActionListener, MouseListener {
    // Main routine that opens an Applet that shows a CheckerBoard
    public static void main(String[] args) {
         new CLayout();

    }
    public static void main1(String[] args)
    {
         JFrame application = new JFrame("Checkers"); // Sets the title at the top of the application as 'Checkers'
         JMenuBar bar = new JMenuBar(); // Adds the Menu Bar
         JMenu fileMenu = new JMenu("File"); // Adds a File Tab to the Menu Bar
         JMenu helpMenu = new JMenu("Help"); // Adds a Help Tab to the Menu Bar
         JMenuItem exit = new JMenuItem("Exit"); // Adds the Exit sub-tab as an Item of the JMenu
         JMenuItem mainMenu = new JMenuItem("Main Menu"); // Adds the Main Menu sub-tab as an Item of the JMenu
         final JMenuItem rules = new JMenuItem("Rules"); // Adds the Rules of Checkers sub-tab as an Item of the JMenu
         final JMenuItem checkersStrategyandTips = new JMenuItem("Checkers Strategy and Tips"); // Adds the Checkers Strategy and Tips sub-tab as an item of the JMenu
         final JMenuItem funFactsaboutCheckers = new JMenuItem("Fun Facts about Checkers"); // Adds the Fun Facts about Checkers sub-tab as an item of the JMenu
         helpMenu.add(funFactsaboutCheckers); // Adds the Fun Facts about Checkers into the Help tab
         helpMenu.add(checkersStrategyandTips); // Adds the How to Play tab into the Help Tab
         helpMenu.add(rules); // Adds the Rules of Checkers tab into the Help tab
         fileMenu.add(mainMenu);// Adds the Main Menu sub-tab into the File tab 
         fileMenu.addSeparator(); // Adds a line in between the Main Menu sub-tab and the Exit sub-tab
         fileMenu.add(exit); // Adds the Exit sub-tab into the Menu tab
         bar.add(fileMenu); // Adds the Menu tab to the Menu bar
         bar.add(helpMenu); // Adds the Help tab to the Menu Bar
         application.setJMenuBar(bar); // Adds the Menu Bar to the application window


        CheckerBoard content = new CheckerBoard(); // Sets the CheckerBoard values into the content to be used in the next line
        application.setContentPane(content); // Container holds the values together, Content pane of the CheckerBoard
        application.pack();  // Use preferred size of content to set size of application.
        Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
        application.setLocation( (screensize.width - application.getWidth())/2,
                (screensize.height - application.getHeight())/2 );
        application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); // Sets so that the application can be exited when the application is closed
        application.setResizable(false);  // This makes it so that the user can't change the application's size.
        application.setVisible(true); // Sets it so that the application can actually be seen
    }
    private JButton NewGameButton;  // Button for starting a new game.
    private JButton ResignButton;   // Button that a player can use to end the game by resigning
    private JLabel MessageDisplay;  // Label for displaying messages to the user.

    public CheckerBoard()  {
// This is going to be a constructor, this constructor will set the layout manager for the panel to be null, it will then add components to the panel
// and it will set their bounds.

    setLayout(null);  // So that it will match my requirement specification, I will do the layout myself
    setBackground(new Color(0,120,0));  // Dark Green Background colour.
    setPreferredSize(new Dimension(350,250)); // The size of the Panel

    BoardComplete checkerboard = new BoardComplete();
    add(checkerboard); // This will create the components and add them to the content pane
    add(NewGameButton);
    add(ResignButton);
    add(MessageDisplay);

 // I will now have to produce a method to set the position and size of each component by calling its setBounds() method
    checkerboard.setBounds(20,20,164,164); // Sets the board dimensions
    NewGameButton.setBounds(210, 60, 120, 30); 
    ResignButton.setBounds(210, 120, 120, 30); 
    MessageDisplay.setBounds(20, 200, 350, 30); 
    }

希望我的目标是可以理解的,任何帮助或建议将不胜感激。谢谢你。我为大量的代码感到抱歉,无论如何,我将非常感谢您的帮助,因为这是我在编码完成之前面临的最后一个问题xD谢谢

娜塔莉·卡洛斯(Natalie Kalos)

您还可以使用类似“棋盘格”的名称显式命名棋盘格面板,因此,当将棋盘格添加到CardPanel时,可以使用“ dummycardpanel.add(checkerboard,“ Checkerboard”);“ 并使用show(JPanel,“ Checkerboard”)作为显示方法,至少对于您要启动游戏的任何按钮(可能不适用于其他按钮,这些按钮很可能需要自己的听众,但这种想法将会大致相同。

最近,我对单个重写的ActionListener进行了类似的操作,并为所有JButton手动设置了ActionCommands,然后在将侦听器分配给我的所有按钮之后,在侦听器中使用了条件分支。这样做的方法不同,但是任何一种方法都可以。我希望这有帮助

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档