gpt4 book ai didi

java - 如何将卡片布局格式添加到我的主游戏屏幕,以便当我单击按钮时它从主菜单屏幕更改为我提供的游戏屏幕代码

转载 作者:太空宇宙 更新时间:2023-11-04 14:03:37 24 4
gpt4 key购买 nike

我正在为我的 A2 计算类(class)制作一款跳棋游戏,该类(class)将在一周内到期。我已经完全完成了游戏,剩下要做的唯一一件事就是通过我制作的 CardLayout 将两个 JPanel 连接在一起。然而,我不确定该怎么做,如果有人能告诉我如何做,我将非常感激,我已经尝试弄清楚它有一段时间了,它只是搞乱了我的结构。谢谢

这是我的 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 谢谢

最佳答案

您还可以使用“Checkerboard”之类的名称明确命名您的 Checkerboard 面板,因此当您将 Checkerboard 添加到 CardPanel 时,您可以使用“dummycardpanel.add(checkerboard, "Checkerboard");”并使用 show(JPanel, "Checkerboard") 作为您的显示方法来执行此操作,至少对于您想要启动游戏的任何按钮(这可能不适用于最有可能需要自己的监听器的其他按钮,但其想法大致相同。

我最近使用一个覆盖的 ActionListener 做了类似的事情,并手动为所有 JButton 设置 ActionCommands,然后在将监听器分配给我的所有按钮后,在监听器内使用条件分支。一种不同的方法,但任何一种方法都可以工作。我希望这有帮助

关于java - 如何将卡片布局格式添加到我的主游戏屏幕,以便当我单击按钮时它从主菜单屏幕更改为我提供的游戏屏幕代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29075796/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com