Jus*_*eff 240 java swing jframe
什么是JFrame
关闭的正确方法,就像用户点击X
关闭按钮或按下Alt+ F4(在Windows上)一样?
我通过以下方式设置我的默认关闭操作:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Run Code Online (Sandbox Code Playgroud)
它完全符合我想要的上述控件.这个问题与此无关.
我真正想要做的是使GUI的行为方式与按下X
关闭按钮会使其表现相同.
假设我要扩展WindowAdaptor
然后添加我的适配器实例作为监听器addWindowListener()
.我想看到的调用相同的序列通过windowDeactivated()
,windowClosing()
以及windowClosed()
作为将与出现X
关闭按钮.可以这么说,撕毁窗户就像告诉它撕裂自己一样.
cam*_*ckr 308
如果您希望GUI的行为就像您单击X
关闭按钮一样,那么您需要将窗口关闭事件分派给Window
.在ExitAction
从关闭应用程序,您可以将此功能添加到一个菜单项或使用任何组件Action
Ş容易.
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
Run Code Online (Sandbox Code Playgroud)
Ale*_*lex 128
setVisible(false); //you can't see me!
dispose(); //Destroy the JFrame object
Run Code Online (Sandbox Code Playgroud)
不太狡猾.
Jam*_*hek 25
如果按住Alt-F4或X你的意思是"立即退出应用程序,而不考虑的是什么其他的Windows或线程正在运行",然后System.exit(...)
会做正是你在一个很突然的,蛮力想要什么,并可能存在问题的方式.
如果按Alt-F4或X表示隐藏窗口,那么frame.setVisible(false)
就是"关闭"窗口的方式.窗口将继续消耗资源/内存,但可以非常快速地再次显示.
如果按Alt-F4或X表示隐藏窗口并处理它正在消耗的任何资源,那么frame.dispose()
就是"关闭"窗口的方式.如果框架是最后一个可见窗口并且没有其他非守护程序线程在运行,程序将退出.如果再次显示该窗口,则必须再次重新初始化所有本机资源(图形缓冲区,窗口句柄等).
dispose()
可能最接近你真正想要的行为.如果您的应用程序打开了多个窗口,您是否希望Alt-F4或X退出应用程序或关闭活动窗口?
窗口监听器上的Java Swing教程可能有助于为您澄清一些事情.
Gaz*_*kus 15
如果您这样做是为了确保用户无法关闭窗口:
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Run Code Online (Sandbox Code Playgroud)
然后你应该改变你的pullThePlug()
方法
public void pullThePlug() {
// this will make sure WindowListener.windowClosing() et al. will be called.
WindowEvent wev = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
// this will hide and dispose the frame, so that the application quits by
// itself if there is nothing else around.
setVisible(false);
dispose();
// if you have other similar frames around, you should dispose them, too.
// finally, call this to really exit.
// i/o libraries such as WiiRemoteJ need this.
// also, this is what swing does for JFrame.EXIT_ON_CLOSE
System.exit(0);
}
Run Code Online (Sandbox Code Playgroud)
我发现这是玩弄漂亮的唯一途径WindowListener
和JFrame.DO_NOTHING_ON_CLOSE
.
这是您的选择:
System.exit(0); // stop program
frame.dispose(); // close window
frame.setVisible(false); // hide window
Run Code Online (Sandbox Code Playgroud)
退出Java运行过程非常简单,基本上你只需要做两件简单的事情:
System.exit(...)
在应用程序的退出点调用java方法.例如,如果您的应用程序是基于帧的,则可以添加侦听器WindowAdapter
并System.exit(...)
在其方法内调用windowClosing(WindowEvent e)
.注意:您必须致电,System.exit(...)
否则您的程序会出错.
System.exit(...)
在正确的位置添加,但这并不意味着可以始终调用该方法,因为意外的java异常可能会阻止调用该方法.这与您的编程技巧密切相关.
**以下是一个最简单的示例(JFrame
基于),它向您展示如何调用exit方法
import java.awt.event.*;
import javax.swing.*;
public class ExitApp extends JFrame
{
public ExitApp()
{
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0); //calling the method is a must
}
});
}
public static void main(String[] args)
{
ExitApp app=new ExitApp();
app.setBounds(133,100,532,400);
app.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 7
不仅要关闭JFrame而且要触发WindowListener事件,请尝试以下方法:
myFrame.dispatchEvent(new WindowEvent(myFrame, WindowEvent.WINDOW_CLOSING));
Run Code Online (Sandbox Code Playgroud)
以编程方式关闭 Swing 框架的最佳方法是使其行为与按下“X”按钮时一样。为此,您需要实现适合您需要的 WindowAdapter,并将框架的默认关闭操作设置为不执行任何操作 (DO_NOTHING_ON_CLOSE)。
像这样初始化你的框架:
private WindowAdapter windowAdapter = null;
private void initFrame() {
this.windowAdapter = new WindowAdapter() {
// WINDOW_CLOSING event handler
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
// You can still stop closing if you want to
int res = JOptionPane.showConfirmDialog(ClosableFrame.this, "Are you sure you want to close?", "Close?", JOptionPane.YES_NO_OPTION);
if ( res == 0 ) {
// dispose method issues the WINDOW_CLOSED event
ClosableFrame.this.dispose();
}
}
// WINDOW_CLOSED event handler
@Override
public void windowClosed(WindowEvent e) {
super.windowClosed(e);
// Close application if you want to with System.exit(0)
// but don't forget to dispose of all resources
// like child frames, threads, ...
// System.exit(0);
}
};
// when you press "X" the WINDOW_CLOSING event is called but that is it
// nothing else happens
this.setDefaultCloseOperation(ClosableFrame.DO_NOTHING_ON_CLOSE);
// don't forget this
this.addWindowListener(this.windowAdapter);
}
Run Code Online (Sandbox Code Playgroud)
您可以通过向其发送 WINDOW_CLOSING 事件以编程方式关闭框架,如下所示:
WindowEvent closingEvent = new WindowEvent(targetFrame, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(closingEvent);
Run Code Online (Sandbox Code Playgroud)
这将关闭框架,就像按下“X”按钮一样。
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
不仅关闭JFrame而且关闭整个应用程序,因此"退出关闭"
要获得相同的结果,您必须有效地退出应用程序,因为只需调用
System.exit(0);
Run Code Online (Sandbox Code Playgroud)
效果完全一样.