如何将卡片布局格式添加到我的主游戏屏幕,以便当我单击一个按钮时它从主菜单屏幕变为我的游戏屏幕代码提供
How to add a cardlayout format to my main game screen so that when I click a button it changes from Main Menu screen into my Game Screen Code Provided
我正在为一周内到期的 A2 Computing 课程作业制作跳棋游戏。我已经完全完成游戏,我唯一要做的就是通过我制作的 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());
}
}
我将显示的代码保持在最低限度,因此我删除了按钮的所有动作侦听器,无论如何我遇到的问题是,我希望这样当用户点击 'Multiplayer' 按钮,它是数组按钮 ourButtons[1],然后它将转换到我的主游戏屏幕,以便用户可以玩跳棋游戏。
这是来自我的 CheckerBoard class 的主要重要 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 设置了 ActionCommand,然后在将侦听器分配给我的所有按钮后在侦听器中使用了一个条件分支。一种不同的方法,但任何一种方法都可以。
希望对您有所帮助
我正在为一周内到期的 A2 Computing 课程作业制作跳棋游戏。我已经完全完成游戏,我唯一要做的就是通过我制作的 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());
}
}
我将显示的代码保持在最低限度,因此我删除了按钮的所有动作侦听器,无论如何我遇到的问题是,我希望这样当用户点击 'Multiplayer' 按钮,它是数组按钮 ourButtons[1],然后它将转换到我的主游戏屏幕,以便用户可以玩跳棋游戏。
这是来自我的 CheckerBoard class 的主要重要 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 设置了 ActionCommand,然后在将侦听器分配给我的所有按钮后在侦听器中使用了一个条件分支。一种不同的方法,但任何一种方法都可以。 希望对您有所帮助