Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I close a JFrame ???

Posted on 2004-10-04
3
Medium Priority
?
218 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 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.
Suggested Courses

618 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