1

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();
            }
        });
    }
}
user2971821
  • 33
  • 2
  • 6
  • 4
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Nov 26 '13 at 09:35
  • 1
    What is the question? – Holger Nov 26 '13 at 10:17
  • Why don't use a modal `JDialog` to show the 3 buttons? If *Start* button is pressed then just dispose this dialog. [How to Use Modality in Dialogs](http://docs.oracle.com/javase/tutorial/uiswing/misc/modality.html) – dic19 Nov 26 '13 at 12:33

1 Answers1

2

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.

Raja Simon
  • 10,126
  • 5
  • 43
  • 74
pooria
  • 343
  • 2
  • 14