meow00
asked on
How do I close a JFrame ???
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(J Frame.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(ActionEven t 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,15 0) ;
restart.addActionListener( this) ;
getContentPane().add(resta rt) ;
// blah ...
}
}
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(J
setSize(425,425) ;
show() ;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Test mySnake = new Test() ;
}
public void actionPerformed(ActionEven
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,15
restart.addActionListener(
getContentPane().add(resta
// blah ...
}
}
you need to keep a reference to the current frame and call its dispose() method to close it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.