Java jdesktop FrameView的windowClosing事件? [英] A windowClosing event for a Java jdesktop FrameView?
问题描述
我正在尝试使用NetBeans编写GUI应用程序,我越来越厌倦了它。
I am trying to write a GUI application using NetBeans, and I am getting increasingly tired of it..
我使用NetBeans构建了一个Java桌面应用程序。它创建主窗体作为 org.jdesktop.application.FrameView
后代。我想知道在表单关闭时添加一些代码的正确方法是什么。
I constructed a "Java Desktop Application" using NetBeans. It creates the main form as a org.jdesktop.application.FrameView
descendent. I would like to know what is the proper way to add some code that is executed when the form is closed.
我目前的尝试是添加一个 WindowAdapter
在构造函数中使用 getFrame()。addWindowListener
的对象,它不起作用。因为在框架尚未构建时无法调用 getFrame
。我不能在GUI构建器的某个地方看到它。
My current attempt so far is to add a WindowAdapter
object using getFrame().addWindowListener
in the constructor, which doesn't work. Because you can't call getFrame
while the frame is not constructed yet. And I can't see it as an event somewhere i the GUI builder.
推荐答案
NetBeans IDE中提供的Java桌面应用程序6.9.1仅用于历史目的,不建议用于项目。当我们尝试使用Java Desktop Application选项创建新项目时,NetBeans IDE 6.9.1也会显示此警告。
Java Desktop Application which is available in NetBeans IDE 6.9.1 is only for historical purposes and is not recommended for use in projects. The NetBeans IDE 6.9.1 also shows this warning when we try to create a new project using Java Desktop Application option.
假设您还在使用Swing应用程序框架,那么让我回答您的问题想要在主窗口中添加一个windowClosing监听器。
Given that let me answer your question assuming you are still using the Swing Application Framework and you want to add a windowClosing listener to the Main Window.
创建Java桌面应用程序时,您将获得三个类(假设您键入了DesktopApplication1作为应用程序的名称):
When you create a Java Desktop Application you get three classes (assuming you typed DesktopApplication1 as the name of your application):
- DesktopApplication1.java
- DesktopApplication1AboutBox.java
- DesktopApplication1View.java
要添加窗口关闭侦听器,请在DesktopApplication1类的configureWindow方法中编写代码,如下所示:
To add the window closing listener, write the code in configureWindow method of the class DesktopApplication1 as follows:
@Override protected void configureWindow(java.awt.Window root) {
root.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// write your code here
System.out.println("Window Closing");
}
});
}
关于
Tushar Joshi,那格浦尔
with regards
Tushar Jos Nagpur
这篇关于Java jdesktop FrameView的windowClosing事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!