-
April 18th, 2009, 11:19 PM
#1
[RESOLVED] using an anonymous inner class
how do i use an anonymous inner class with this code:
Code:
class MyFrame extends JFrame{
MyListener listener = new MyListener();
public MyFrame(){
addWindowListener(listener);
}
}
class MyListener extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
-
April 19th, 2009, 07:05 AM
#2
Re: using an anonymous inner class
If you want to exit the application when the main window frame closes, the usual way is to set the frame's default close operation to DISPOSE_ON_CLOSE (or for uncontrolled exit, EXIT_ON_CLOSE). See the JFrame API docs for details:
Code:
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Remember that (as we already told you) you should avoid using System.exit(), especially in GUI applications.
If you want to add a WindowListener using an anonymous class, it would go something like this:
Code:
public MyFrame(){
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.out.println("Window is closing");
}
});
}
The problem is never how to get new, innovative thoughts into your mind, but how to get old ones out!
D. Hock
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|