?
Solved

How do I close a JFrame ???

Posted on 2004-10-04
3
Medium Priority
?
219 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
  • 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 1000 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 200 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

850 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