【问题标题】:On button click, close the application Java GUI单击按钮时,关闭应用程序 Java GUI
【发布时间】:2014-11-24 14:04:03
【问题描述】:

我有一个 java 应用程序,想要关闭带有确认窗口的 GUI 以关闭应用程序

例如:-

        frmViperManufacturingRecord.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frmViperManufacturingRecord.addWindowListener( new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                JFrame frame = (JFrame)e.getSource();

                int result = JOptionPane.showConfirmDialog(frame, "Are you sure you want to close the application?", "Please Confirm",JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_OPTION)
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
            });

这是工作fine,当我按下窗口关闭 (x) 按钮时,但我想将此事件带到一个按钮以执行“单击时”操作,因为我是新手,很难把它带进去'actionPerformed'

到目前为止,我已经尝试了下面的代码,但它没有工作......

        //close window button
        JButton btnCloseWindow = new JButton("Close Window");
        btnCloseWindow.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //frmViperManufacturingRecord.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                //frmViperManufacturingRecord.dispose();
                //System.exit(0);
                JFrame frame = (JFrame)e.getSource();

                int result = JOptionPane.showConfirmDialog(frame, "Are you sure you want to close the application?", "Please Confirm",JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_OPTION)
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
        });

请给我一些指导,谢谢

【问题讨论】:

  • 感谢@user2408578,System.exit(0) 工作正常,但在关闭应用程序之前需要一个确认窗口,谢谢
  • 没听懂我说试试 if (result == JOptionPane.YES_OPTION){ } ....
  • 谢谢@user2408578,它给了error,谢谢

标签: java swing user-interface


【解决方案1】:

改变这个:

if (result == JOptionPane.YES_OPTION)
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

if (result == JOptionPane.YES_OPTION)
  System.exit(0);
}

frame.setDefaultCloseOperation 是当有人单击“x”关闭窗口时发生的情况。其他所有退出方式都由您控制。最好的方法是让您的窗口关闭侦听器和动作侦听器调用相同的方法,该方法将弹出对话框并在用户想要退出时调用System.exit(0)。这也将帮助您进行清理操作。

示例代码:

public class Test extends JPanel implements WindowListener {
    public Test() {
        setLayout(new BorderLayout());
        add(new JLabel("This is a test."), BorderLayout.CENTER);
        JButton b = new JButton("Exit");
        b.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                exit();

            }
        });
        add(b, BorderLayout.SOUTH);
    }
    public static void main(String[] args) throws Exception {
        JFrame f = new JFrame();
        Test t = new Test();
        f.add(t, BorderLayout.CENTER);
        f.addWindowListener(t);
        f.pack();
        f.setVisible(true);
    }
    public void windowClosing(WindowEvent e) {
        exit();

    }

    private void exit() {
        System.exit(0);
    }
}

【讨论】:

  • 感谢@markbernard,已经尝试过了,但它会抛出一个错误,线程“AWT-EventQueue-0”中的异常。 error
  • System.exit(0) 不会导致 ClassCastException。一定有其他问题。我添加了一些代码来展示一个工作示例。
  • 感谢@markbernard,错误不在 System.exit(0) 中,但@第 122 行是 user2902894 指出的错误“JFrame frame = (JFrame)e.getSource ();",已更改为 "JFrame frame = new JFrame();",现在可以使用,非常感谢您的时间和帮助。
【解决方案2】:

你可以试试:

if (result == JOptionPane.YES_OPTION){
                    frame.dispose();
            }

还要注意第 122 行的 CastException。

代替

JFrame frame = (JFrame)e.getSource();

改为:

JFrame frame = new JFrame();

【讨论】:

  • 感谢@user2902894,我确实尝试了您的两个选项 System.exit(0) 和 frame.dispose();两者都抛出error,谢谢
  • 它似乎在其他行抛出错误,你能发布抛出这个异常的行吗?
  • 感谢@user2902894,错误第122行是“JFrame frame = (JFrame)e.getSource();”并在此处发布了完整的code,感谢您的时间和帮助
  • 问题出在你的选角上。不能将 ActionEvent 类型的对象强制转换为 JFrame。
  • 更改 JFrame 框架 = (JFrame)e.getSource();在第 122 行进入:JFrame frame = new JFrame();
【解决方案3】:

如果还是不行就试试

frame.setVisible(false);
frame.dispose();

在 if(result == JOptionPane.YES_OPTION){} 块中

【讨论】:

  • 谢谢@user2408578,仍然给出同样的错误。谢谢
  • 什么是异常...请粘贴堆栈跟踪
  • 谢谢@user2408578,已经粘贴了完整的stackTrace,谢谢
  • 阅读这一行“java.lang.ClassCastException: javax.swing.JButton cannot be cast to javax.swing.JFrame”。所以你不能将按钮(即当前事件)转换为 jframe.. .
  • 只需评论这一行并在 if 块中尝试 System.exit(0) ......它应该可以工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-23
  • 2017-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多