简体   繁体   中英

How to make a start menu with another frame in Java?

So I'm making a game and It's working quite well, but I just need to have a start menu with 3 buttons. A Play, Instructions and Exit button. The problem is that I want it in a different frame than the game itself, if you press on Start the frame should switch to the game frame. Can someone help me with this? This is a part of my code:

package frametest;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.*;

public class FrameTest extends JFrame implements ActionListener {
    public JPanel createContentPane() {
        //Jpanels and stuff
    }

    public JPanel createMainMenu() {
        //Start menu JPanels and stuff
    }

    private static void createAndShowGUI() {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("Poker Game");

        FrameTest demo = new FrameTest();
        frame.setContentPane(demo.createContentPane());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        frame.setSize(1080,640);
        frame.setVisible(false);

        JFrame menu = new JFrame("Poker Game");

        menu.setContentPane(demo.createMainMenu());

        menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        menu.setSize(1080,640);
        menu.setVisible(true);
    }

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

You must have a setting frame class for your main function and essential for frame and you should have another class for game controlling and a class for menu.

You should get instant of game controller in frame and do the same in controller for menu then you can have separate frame that you want.

please use interfaces to have a clean code .

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