gpt4 book ai didi

java - Swing : Exit application actionListener

转载 作者:行者123 更新时间:2023-11-30 02:23:12 25 4
gpt4 key购买 nike

我试图弄清楚如何通过单击按钮退出应用程序。我遇到的导致我无法退出应用程序的问题是因为我正在使用主类中的“extend JFRame”。

举个例子,

app.class

public class app{
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
JFrame frame = new MainFrame("Exercise one");
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
.. .. ..
frame.setVisible(true);
})
}
}

MainFrame.java

public class MainFrame extends JFrame(){
public MainFrame(String title){
super(title)

//set layout manager
setLayout(new BorderLayout());

//swing components
JButton exit = new JButton("Exit");

//add container
Container container = getContentPane();

container.add(exit);

//create actionlist logic
exit.addActionListener(new ActionListener()){
@Override
public void actionPerformed(ActionEvent arg0){
// on click , this logic will end the application
}
}

}
}

我完全理解如何从应用程序类中取消应用程序。但是在我想从 MainFrame 中取消应用程序的情况下。可以做到吗?

提前谢谢您。

最佳答案

defaultCloseOperation 仅在遇到 WINDOW_CLOSING 事件时由框架处理,setVisibledispose 都不会处理触发此事件,这意味着 defaultCloseOperation 将不会被处理

确保触发此操作的唯一方法是手动调度 WINDOW_CLOSING 事件

frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));

想要遵循此路径的主要原因之一是,它确保应用程序遵循配置的 defaultCloseOperation 并做出自己的决定(例如调用 System.exit > 手动)

下面演示了隐藏、处理和调度的方法。只有调度方法才会关闭窗口并终止 JVM

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.AbstractAction;
import static javax.swing.Action.NAME;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;

frame.add(new JButton(new HideAction(frame)), gbc);
frame.add(new JButton(new DisposeAction(frame)), gbc);
frame.add(new JButton(new DispatchAction(frame)), gbc);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("Closing");
}

@Override
public void windowClosed(WindowEvent e) {
System.out.println("Closed");
}
});
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class HideAction extends AbstractAction {

private JFrame frame;

public HideAction(JFrame frame) {
this.frame = frame;
putValue(NAME, "Hide");
}

@Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
}

}

public class DisposeAction extends AbstractAction {

private JFrame frame;

public DisposeAction(JFrame frame) {
this.frame = frame;
putValue(NAME, "Dispose");
}

@Override
public void actionPerformed(ActionEvent e) {
frame.dispose();
}

}

public class DispatchAction extends AbstractAction {

private JFrame frame;

public DispatchAction(JFrame frame) {
this.frame = frame;
putValue(NAME, "Dispatch");
}

@Override
public void actionPerformed(ActionEvent e) {
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}

}
}

如果我在 IDE 中运行此命令,除非使用分派(dispatch)选项,否则 JVM 会保持运行状态,并且我必须终止 session 才能完全关闭它。

我还注意到,调用dispose只会触发WINDOW_CLOSED事件,而dispatch方法会触发WINDOW_CLOSING事件

关于java - Swing : Exit application actionListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46273068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com