[英]How to close window (Java Swing) after run?
I have standard Swing GUI class:我有标准的 Swing GUI 类:
public class ElevatorGUI extends JFrame implements Observer {
private JButton button2;
private JTextArea textArea2;
private JSeparator separator2;
private JLabel label4;
private JLabel label5;
private JLabel label6;
with init用初始化
Container contentPane = getContentPane();
GroupLayout contentPaneLayout = new GroupLayout(contentPane);
contentPane.setLayout(contentPaneLayout);
contentPaneLayout.setHorizontalGroup(
contentPaneLayout.createParallelGroup()
.addGroup(contentPaneLayout.createSequentialGroup()
... etc. So we can to close this GUI by standard way: ...等所以我们可以通过标准方式关闭这个GUI:
setDefaultCloseOperation(EXIT_ON_CLOSE);
but how to do the same thing from method?但是如何从方法中做同样的事情呢? For example:例如:
public void update() {
Thread.currentThread().interrupt();
}
Taken from How to programmatically close a JFrame :摘自How to programmatically close a JFrame :
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
When the class extends JFrame
, do this instead:当该类扩展JFrame
,请改为执行以下操作:
this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
When this is executed the JFrame will be closed exactly as if the user pressed the "x" button.执行此操作时,JFrame 将完全关闭,就像用户按下“x”按钮一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.