• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 917
  • Last Modified:

How to restart Java GUI application?

So I ran into this problem while creating a game.  When the user loses he's given the option to play again.  If he says yes, I have to set everything as a new game.

How do I go about doing this?

I mean, is there a simple way to do it?  I have like 100 object and it'd be ridiculous to make new objects.
0
ubuntuguy
Asked:
ubuntuguy
  • 4
  • 3
1 Solution
 
hadesflames5855Commented:
try putting it in a while loop. when the game is over, the user is asked to start again, if the user says yes, you start the loop over, else you end the loop and the program is over.
while(a){
//game code
String x = JOptionPane.showInputDialog(null,"Would you like to play again?");
if(x.equalsIgnoreCase("no")
   a = false;
}
JOptionPane.showMessageDialog("Thank you for playing!");

Open in new window

0
 
ubuntuguyAuthor Commented:
that solution seems to complicated for what i have.  got a game with like 3000 lines of code.  it'll just confuse me.
0
 
hadesflames5855Commented:
not really, you would put the while loop in the main method, and set the frame visiable inside the loop, along with the rest of the game code and pretty much everything.

also instead of the above code also, do this:
while(a){
//game code
Object[] x = {"Yes","No"};
		int y = JOptionPane.showOptionDialog(null, "Would you like to play again?", "My Game", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, x, x[0]);
if(y == 1)
  a = false;
}
JOptionPane.showMessageDialog("Thank you for playing!");

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ubuntuguyAuthor Commented:
In my main method?

This is my main method.... I ask the user if they want to keep playing in the game class... so not sure how I would do it inside the main method.


 
public class BattleshipApp {
 
 
 
	public static void main(String[] args) {
		
			
			GameBoard g = new GameBoard();
			g.setVisible(true);
			g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			
		
			
		}
 
	}

Open in new window

0
 
hadesflames5855Commented:
try this and tell me if it works:


while(a){
GameBoard g = new GameBoard();
g.setVisible(true);                  g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
Object[] x = {"Yes","No"};
int y = JOptionPane.showOptionDialog(null, "Would you like to play again?", "My Game", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, x, x[0]);
if(y == 1)
  a = false;
}
JOptionPane.showMessageDialog("Thank you for playing!");

Open in new window

0
 
ubuntuguyAuthor Commented:
Thanks for your help. You code kept showing the would you like to play again message in an infinite loop.  I had to turn in my project already so I just sent what I had.  Thanks for your help again.
0
 
hadesflames5855Commented:
well, did you have a declared as boolean a = true; before the while? I assumed you would do so yourself...I tested this code myself and does not lead to an infinite loop....
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now