在 Java 中处理和关闭窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4737495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disposing and closing windows in Java
提问by Keysmack
Okay, so this might be a stupid question, but I'm new to Java and trying to teach myself things the right way before I develop any bad habits.
好的,所以这可能是一个愚蠢的问题,但我是 Java 新手,并试图在养成任何坏习惯之前以正确的方式自学。
Anyway, I was writing a program last night that consisted of a custom class extending Frame and a custom class extending Canvas. The main() method is in the canvas class and I create an instance of the frame class there. The problem is that when the program detects a window close event, I can't dispose the frame because I seemingly have no way to access it from outside the main method. And if I try to define it outside of main(), then I can't use it within. So I ended up skipping dispose() and just using System.exit(0). Is this alright? Is it basically doing the same thing anyway? Or is this a problem I need to fix, and if so, any idea how?
无论如何,我昨晚正在编写一个程序,它由一个扩展 Frame 的自定义类和一个扩展 Canvas 的自定义类组成。main() 方法在画布类中,我在那里创建了框架类的一个实例。问题是当程序检测到窗口关闭事件时,我无法处理该框架,因为我似乎无法从 main 方法外部访问它。如果我尝试在 main() 之外定义它,那么我就不能在里面使用它。所以我最终跳过了 dispose() 而只是使用 System.exit(0)。这可以吗?无论如何,它基本上都在做同样的事情吗?或者这是我需要解决的问题,如果是这样,知道如何解决吗?
Thanks so much for reading,
非常感谢您的阅读,
Cody
科迪
回答by finnw
You can get a reference to the frame, from the source
property of the event:
您可以从source
事件的属性中获取对框架的引用:
class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e){
Frame frame = (Frame) e.getSource();
frame.dispose();
}
}
Alternatively, since this is an anonymous class (presumably) declared within the constructor, you also have access to the enclosing instance, so you can also write it as:
或者,由于这是在构造函数中声明的匿名类(大概),您还可以访问封闭实例,因此您也可以将其编写为:
class MyFrameClass extends Frame {
public MyFrameClass() {
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
MyFrameClass.this.dispose();
}
});
}
}
Or you can make it simpler still (as your WindowListener does not have a method of its own called "dispose"):
或者你可以让它更简单(因为你的 WindowListener 没有一个叫做“dispose”的方法):
public void windowClosing(WindowEvent e){
dispose();
}
回答by Vincent Ramdhanie
Not a stupid question. Because of the garbage collector its not such a big issue, however, there are some times when you will want to execute some cleanup when a window closes. So some suggestions:
不是一个愚蠢的问题。由于垃圾收集器并不是什么大问题,但是,有时您会希望在窗口关闭时执行一些清理工作。所以一些建议:
The Window Closing event should be handled from the Frame itself. For instance:
Window Closing 事件应该从 Frame 本身处理。例如:
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
//code here to perform as the window is about to close.
}
});
And I would suggest that you create a separate class for your main method that will invoke the Frame etc.
我建议您为将调用 Frame 等的主要方法创建一个单独的类。
回答by sathya
This is used to close Jframe with an event handler.
current Jframe
public class LoginForm extends JFrame
{
LoginForm()
{
//Some code for Jframe and its components.
if(Condition)
disposewindow();
}
private void disposewindow()
{
WindowEvent closingEvent = new WindowEvent(LoginForm.this,
WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(closingEvent);
}
//you can can use for alternate of dispose()event and it post some event handler **Closing event** ,
// if we can use this closing event to open new window with conditions.
//It means closing child window with closing event, get this flag in main window to make main window as Disable or Enable state
}
}
//In parent window @Override
//在父窗口@Override
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
this.frame.disable();
}