0
\$\begingroup\$

I have written a launcher that prompts the user for their name then starts the game when they hit enter. The launcher looks like this:enter image description here

But when the user hits enter, this happens:enter image description here

The launcher stays open.

This is the relevant code in the launcher class:

    public void actionPerformed(ActionEvent evt) {
        String text = textField.getText();
        System.out.println(text);
        entered=true;
        Game game = new Game();
        game.start(text);
    }

    public void Launch() {
        //Create and set up the window.

        JFrame frame = new JFrame("Login");
     // maximize the jframe here
        frame.setSize(800,600);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Add contents to the window.
        frame.add(new Launcher());
        //Display the window.
        frame.pack();
        frame.setVisible(true);
        while(entered==true){}
    }

And this is for the main method in the game:

    public static void main(String[] argv) {
        Launcher launcher = new Launcher();
        launcher.Launch();
    }

So how would I go about closing the launcher when the user has no use for it?

I am using a JFrame for the launcher and Lwjgl for the actual game window.

\$\endgroup\$
  • \$\begingroup\$ I've edited your question to remove the irrelevant code. When you include all your code, it looks like you don't have any idea what you're doing and clutters the question. For example, in a problem like this, we can assume that you have all the required include statements, there's no need to include them, same goes for your rendering code. \$\endgroup\$ – MichaelHouse Mar 8 '13 at 15:28
1
\$\begingroup\$

You want to think more about what your actual problem is. Always try to boil down these larger issues into smaller bits. In this case, your JFrame is staying open. That's expected behavior, since you haven't tried to close it!

So the problem isn't that your JFrame is staying open, it's that you don't even try to close it. Check out this question on SO for programmatically closing a JFrame.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ So would I add frame.dispose(); after game.start(text); \$\endgroup\$ – user25855 Mar 8 '13 at 15:31
  • \$\begingroup\$ Griffy, best way to find out is to try! I'm trying to be less direct in my answers to you, I want you to learn how to find help yourself first since it's an important skill for you to learn. Good luck with your game! \$\endgroup\$ – MichaelHouse Mar 8 '13 at 15:32
  • \$\begingroup\$ I can't get anything to work! :( \$\endgroup\$ – user25855 Mar 8 '13 at 16:16
  • \$\begingroup\$ You'll need to be specific about what you've tried and what exactly isn't working. Take it one step at a time, try the most basic approach first and then build up from there once it's working. \$\endgroup\$ – MichaelHouse Mar 8 '13 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy