简体   繁体   中英

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

I'm making a game of checkers for my A2 Computing coursework which is due in within a week. I have completely finished the game, and only thing I have left to do is connect the two JPanels together via a CardLayout that I have made. However I am unsure how to do so, if anyone could possibly tell me how I would be extremely grateful and I've been trying to figure it out for awhile and It just is messing up my structure. Thank you

Here is the code from my 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());

}
}

I have kept the code I am displaying to a minimal, hence I have removed all the action listeners for my buttons, anyway the problem I have is that, I would like it so that when the user clicks on the 'Multiplayer' button which is the array button ourButtons[1], it will then transition into my main game screen so that the user can then play a game of checkers.

Here is the main important GUI from my CheckerBoard class:

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); 
    }

Hopefully what I am aiming for is understandable, any help or advice would be extremely appreciated. Thank you. I'm sorry for the large amount of code, anyway I would be extremely grateful for any help as this is the last problem that I am facing before my coding is finished xD Thank you

you can also explicitly name you Checkerboard panel with something like "Checkerboard", so when you add the Checkerboard to your CardPanel, you can use"dummycardpanel.add(checkerboard, "Checkerboard");" and use show(JPanel, "Checkerboard") as your show method to do it, at least for whatever button you want to fire up the game(that may not work for other buttons which will need their own listeners most likely, but the idea will be mostly the same.

I did something similar recently with a single overridden ActionListener and set the ActionCommands for all my JButtons manually, then used a conditionals branch inside the listener after assigning the listener to all my buttons. a different way of doing it, but either way can work. I hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM