0

So i made this game type program that restarts the game once you click on reset button but idk how to make it functional.. This is what i did but the only thing it does is creates a new java program of the game. It doesn't get rid of the old one or anything.. here is my code for restarting.. What codes should i add :/

button1.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        new Tests();
    }
});
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
the feels
  • 107
  • 1
  • 7
  • 1
    "but idk how to make it functional." it depends what you mean why functional, this term can mean a lot in terms of programming. – Ousmane D. Apr 17 '17 at 13:22
  • 2
    have you searched "reopen java program" in google? – ItamarG3 Apr 17 '17 at 13:22
  • 1
    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. – Ousmane D. Apr 17 '17 at 13:23
  • @OusmaneMahyDiaw I mean like i know how to put the button but idk how i can restart the game by clicking on it – the feels Apr 17 '17 at 13:23
  • @ItamarGreen i googled restart java program but found nothing :/ – the feels Apr 17 '17 at 13:24
  • @FredJohn refer to this: http://stackoverflow.com/questions/4159802/how-can-i-restart-a-java-application – ItamarG3 Apr 17 '17 at 13:27
  • @FredJohn: There's no one-size-fits-all solution. You need to give your GUI a reset method of some sort and have this listener call it, and how it works, how it is wired is all dependent on the structure of your program. Myself, I'd strive to make my program in a model-view structure or one of its variants where the program logic (the model) is separate from the GUI code (the view), and then my reset method would tell the model to reset itself (again all depending on how I structure this model) and then this would cause changes to the view. TL;DR: short answer -- your question is unanswerable – Hovercraft Full Of Eels Apr 17 '17 at 13:45
  • Except that what you **don't** want to do is to create a new JFrame or other window. – Hovercraft Full Of Eels Apr 17 '17 at 13:46
  • @HovercraftFullOfEels i heard u can use frame.dispose(); method but the thing is, my program isn't able to detect the already declared frame outside the actionListener method so when i type in frame.dispose(); i get an error – the feels Apr 17 '17 at 13:50
  • No, again you're looking for ready made Swing solutions and that is not what you should be doing. Again, you will want to reset your program to its initial conditions in that method, not disposing of JFrames. Look to your own code, not the Swing library for your solution. – Hovercraft Full Of Eels Apr 17 '17 at 13:52

0 Answers0