Solved

How do I close a JFrame ???

Posted on 2004-10-04
3
216 Views
Last Modified: 2008-02-26
Hi Experts,

   In the end following code, every time when the gameOver() is called, it creates a new Test(). Therefore, after several runs ... I got many Test() and many JFrame .... How do I close the previous one after a new one is created ??? many thanks !!!


public class Test extends JFrame implements KeyListener, ActionListener{
           
         // --- blah blah ....
 
          public Test() {

         // --- blah blah ....                      
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(425,425) ;
        show() ;
       
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Test mySnake = new Test() ;
       
    }
   
    public void actionPerformed(ActionEvent e) {
               blah .....    
         if(e.getSource() == restart){ Test PlayAgain = new Test() ;}
               blah .....
   }

      public void gameOver(){
           //   blah ....  
        restart = new JButton() ;
        restart.setText("Play Again!") ;
        restart.setSize(150,30) ;
        restart.setLocation(125,150) ;
        restart.addActionListener(this) ;
        getContentPane().add(restart) ;
          // blah ...
       }
}

0
Comment
Question by:meow00
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12223378
you need to keep a reference to the current frame and call its dispose() method to close it.
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 12223410
instead of creating a new window, it may be a better idea to instead clear/reset the current one.
0
 
LVL 3

Assisted Solution

by:CI-Ia0s
CI-Ia0s earned 50 total points
ID: 12223412
If you may need the frame later you can just make it hide with "frameName.setVisible(false);", though this still ties up its resources. If you want to get rid of it for good, use dispose() as objects suggested (note that all of it's component and any variables you might have declared within an extended JPanel, etc. are also erased).
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question