-2

I've setup my Jframe and started coding for my game prototype. I can start the game directly just fine but now trying to build a main menu for my game. I've setup my menu main with Jbuttons and my quit button works as I need. Now I'm trying to get my Start button to start my game. Trying to code to close out the mainmenu class and start the class I've setup to run my game. Here is my code snip...

public class Frame extends JFrame{
    //snip

MainMenu mainMenu;
Screen screen;

public Frame(){
    init();     
}

public void init(){
    JPanel panel = new JPanel();
    getContentPane().add(panel);
    panel.setLayout(null);      
    xValue = 800;
    yValue = 600;
    size = new Dimension (xValue, yValue);
    setTitle(title);
    setSize(size);
    setResizable(false);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
    mainMenu = new MainMenu(this);
    add(mainMenu);      
    setVisible(true);
}

public void startGame(){
     //Trying to close MainMenu class via below code but appears to do nothing yet system.out is being called
    this.remove(mainMenu);
    mainMenu.dispose();
    System.out.println("I was clicked");
    screen = new Screen(this); //this is my class to run the game
    this.add(screen);
}

public static void main(String args[]){
    Frame frame = new Frame();
}
}

Then my MainMenu class that I'm trying to close so I can then start my game class...

public class MainMenu extends JPanel implements ActionListener {

    public static int myWidth, myHeight;
    public static int stringWidth, stringHeight;
    public static int buttonSpace;  
    public static Frame frame;  
    public Font smallFont,largeFont;    
    public static JButton startButton, continueButton, optionButton, exitButton;    
    public static boolean isFirst = true;   
    public static Point mse = new Point(0,0);

    public MainMenu(Frame frame) {
        frame.addKeyListener(new Listener());
        frame.addMouseListener(new Listener());
        frame.addMouseMotionListener(new Listener());
        buildButton();
        frame.add(startButton);
        frame.add(continueButton);
        frame.add(optionButton);
        frame.add(exitButton);
    }

    public void define(Graphics g){
        //frame = new Frame(); Trying to init frame to prevent a nullpointexception error but causes the problem of opening a second jframe
        //snip code
    }

    public void paintComponent(Graphics g){
        //snip code
    }

    public void buildButton(){
        startButton = new JButton("Start");
        startButton.setBounds(325, 200, 150, 50);
        startButton.setRolloverEnabled(true);
        startButton.addActionListener(new ActionListener() {
               @Override
               public void actionPerformed(ActionEvent event) {
                   frame.startGame(); //want to start game code from here

              }
           });

                //snip code

        exitButton = new JButton("Quit");
        exitButton.setBounds(325, 200 + 180, 150, 50);
        exitButton.setRolloverEnabled(true);
        exitButton.addActionListener(new ActionListener() {
               @Override
               public void actionPerformed(ActionEvent event) {
                   System.exit(0);
              }
           });

    } //snip...

I've tried to google my issue but can't find anything other then opening up more Jframes (probably searching with the wrong words). I don't want to do this. Just trying to do what other games do with a mainmenu that allows a entry point into the game itself.

millimoose
  • 39,073
  • 9
  • 82
  • 134
maebe
  • 553
  • 1
  • 6
  • 18
  • 3
    1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) 2) Don't mix Swing (e.g. `JPanel`) & AWT (e.g. `Frame`) components without good cause. In this case, use a Swing based `JFrame`. 3) Please use code formatting for code, input/output & structured documents like HTML or XML. To do that, select the sample and click the `{}` button above the messaged posting/editing form. 4) Don't set the size of top level containers. Instead layout the content & call `pack()`. – Andrew Thompson Jun 24 '13 at 04:24
  • 1
    Ditto to all the great suggestions made by @Andrew. 1) Also, don't over-use and especially don't inappropriately use the static modifier as it will make your program difficult to extend, debug, test, and maintain. 2) Avoid null layout and `setBounds(...)` as much as possible. Learn to use the layout managers to help you create pleasing, self-sizing complex yet harmonious GUI's that re-size cleanly and work well on any platform. – Hovercraft Full Of Eels Jun 24 '13 at 04:29
  • 2
    @HovercraftFullOfEels Yes, good catch. The *vast majority* of times new programmers uses the `static` keyword it is a) Unnecessary. b) The source of lots of bugs. – Andrew Thompson Jun 24 '13 at 04:32
  • @Andrew Read the tutorial in 1 the other night. Seems like multi Jframes is counter intuitive as a long time game and something I would never want to mess with. #2 will do some searches into this and should help me a lot. #3 & 4 do you have more information on this that can be linked as not 100% sure what you mean. On #4 I think you mean the size of my Jframe. If that is the case I want to have an option to allow user to set the resolution. Which is soon on the list to code via a option menu. I'll give "java pack()" a google search but if you know of any good tutorials I'll take a look – maebe Jun 24 '13 at 04:37
  • 3
    @Hover & Andrew About static. Think that is a habit I've got myself into from different tutorials. Most don't explain it very well. Was months into learning java/android before I even knew what it did. Also a lot of times eclipse will give me error about "accessing a static reference in a nonstatic way." And making the item "static" is a quick/dirty way of moving forward with the code. I will trying learning more about this issue and break myself of that habit. – maebe Jun 24 '13 at 04:43
  • 3) is about how to use the forum & make posts. 4) is basically saying - if the content area needs to be 800x600, set the content area (e.g. a `JPanel`) to have a *preferred* size of 800x600, then add it to a frame (using a layout) and call `ourFrame.pack();` Doing so will cause the frame to be set to the *minimum* size it needs to be, in order to display the content as well as ***its own icons, menus, borders & other decorations.*** Obviously, the size of the entire frame will end up *larger* than that set for the content. – Andrew Thompson Jun 24 '13 at 04:45
  • @maebe: I really like what you're saying here. If you follow through with it, you won't regret it and will progress immensely with your coding progression. Luck! – Hovercraft Full Of Eels Jun 24 '13 at 04:46
  • Re: 'Static Trap' It definitely is a trap that new programmers fall into, I remember doing that when I started out but if you learn to break the habit and what `static` does and is used for then you will benefit greatly – Java Devil Jun 24 '13 at 04:54
  • @Hover & Andrew I'm loving all the input on how to code better. You two have given me some great things to research in my free time (which is small working 65 hours a week). Bringing it back to the question that had me post this question. About closing one class and starting another. Is this something I will find out learning more about java.swing? I started a tutorial last night about swing but haven't finished it yet. Or is this something separate that I'll have to look into (android made is so much easier/I'm not seeing the equivocate code). – maebe Jun 24 '13 at 04:57

1 Answers1

0

Code your user interface as a series of modules/components, each anchored on a JPanel. Then swap out those modules as needed to display what user interface is needed at any particular moment.

jwenting
  • 5,505
  • 2
  • 25
  • 30