Solved

How do I close a JFrame ???

Posted on 2004-10-04
3
210 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 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Increment alphanumeric sequence 6 81
HashMap Vs TreeMap 12 49
Problem to start Neon 20 54
Java Jpanels and Jframe 8 21
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now