Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 改为设置DefaultCloseOperation以显示JFrame_Java_Swing_Jframe_Windowlistener - Fatal编程技术网

Java 改为设置DefaultCloseOperation以显示JFrame

Java 改为设置DefaultCloseOperation以显示JFrame,java,swing,jframe,windowlistener,Java,Swing,Jframe,Windowlistener,为了练习Java,我正在制作一个字处理器应用程序,我希望这样,当用户试图关闭应用程序时,会出现一个JFrame请求保存更改 我正在考虑setDefaultCloseOperation(),但到目前为止我运气不好。如果可能的话,我也希望在用户单击窗口右上角的“X”时显示它。您可以将JFrame DefaultCloseOperation设置为类似“不做任何事情”,然后设置WindowsListener以捕获关闭事件并执行所需操作。我将在几分钟后发布一个示例 编辑:以下是示例: public sta

为了练习Java,我正在制作一个字处理器应用程序,我希望这样,当用户试图关闭应用程序时,会出现一个JFrame请求保存更改


我正在考虑setDefaultCloseOperation(),但到目前为止我运气不好。如果可能的话,我也希望在用户单击窗口右上角的“X”时显示它。

您可以将JFrame DefaultCloseOperation设置为类似“不做任何事情”,然后设置WindowsListener以捕获关闭事件并执行所需操作。我将在几分钟后发布一个示例

编辑:以下是示例:

public static void main(String[] args) {
        final JFrame frame = new JFrame("Test Frame");

        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        frame.setSize(800, 600);

        frame.addWindowListener(new WindowAdapter() {
            //I skipped unused callbacks for readability

            @Override
            public void windowClosing(WindowEvent e) {
                if(JOptionPane.showConfirmDialog(frame, "Are you sure ?") == JOptionPane.OK_OPTION){
                    frame.setVisible(false);
                    frame.dispose();
                }
            }
        });

        frame.setVisible(true);
    }
  • 您必须将
    WindowListener
    添加到
    JFrame

  • 窗口关闭
    方法中,您可以提供所需的代码

例如:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ClosingFrame extends JFrame {

    private JMenuBar MenuBar = new JMenuBar();
    private JFrame frame = new JFrame();
    private static final long serialVersionUID = 1L;
    private JMenu File = new JMenu("File");
    private JMenuItem Exit = new JMenuItem("Exit");

    public ClosingFrame() {
        File.add(Exit);
        MenuBar.add(File);
        Exit.addActionListener(new ExitListener());
        WindowListener exitListener = new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                int confirm = JOptionPane.showOptionDialog(frame,
                        "Are You Sure to Close this Application?",
                        "Exit Confirmation", JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE, null, null, null);
                if (confirm == JOptionPane.OK_OPTION) {
                    System.exit(0);
                }
            }
        };
        frame.addWindowListener(exitListener);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setJMenuBar(MenuBar);
        frame.setPreferredSize(new Dimension(400, 300));
        frame.setLocation(100, 100);
        frame.pack();
        frame.setVisible(true);
    }

    private class ExitListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            int confirm = JOptionPane.showOptionDialog(frame,
                    "Are You Sure to Close this Application?",
                    "Exit Confirmation", JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null, null, null);
            if (confirm == JOptionPane.OK_OPTION) {
                System.exit(0);
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ClosingFrame cf = new ClosingFrame();
            }
        });
    }
}

1) ITYM
frame.addWindowListener(新的WindowAdapter(){
(否则是编译错误)。2)由于EDT仍在运行,JRE不会结束。以下是调用
System.exit(0)的罕见情况之一
可能是必需的。@AndrewThompson您认为关闭应用程序而不是系统最安全的方法是什么。退出(0);?好的,您可以首先查看正在运行的线程,并检查它是否只有EDT仍在运行。如果是这样,使用
System.exit(0)结束JVM是非常安全的
。但是这里有一个更好的解决方案,请参见我的答案。@andrewhompson 1)WindowListener工作正常,是addWindowListener中定义的参数类型。2)EDT在帧被释放后停止(JRE正确结束),但可能在某些情况下它不会(永远不会满足)。“有些情况下,它不会“例如,我的甲骨文提供的JRE。”(从来没有遇到过)。“来我家吧!带啤酒来。”。。
import java.awt.event.*;
import javax.swing.*;

public class QuickGuiTest {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                final JFrame frame = new JFrame("Test Frame");

                frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                frame.setSize(600, 400);
                frame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        int result = JOptionPane.showConfirmDialog(
                                frame, "Are you sure?");
                        if( result==JOptionPane.OK_OPTION){
                            // NOW we change it to dispose on close..
                            frame.setDefaultCloseOperation(
                                    JFrame.DISPOSE_ON_CLOSE);
                            frame.setVisible(false);
                            frame.dispose();
                        }
                    }
                });
                frame.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}