Java 当用户试图关闭窗口按钮时,如何检查文件是否保存?

Java 当用户试图关闭窗口按钮时,如何检查文件是否保存?,java,swing,file-io,jframe,Java,Swing,File Io,Jframe,我有几节课,但我只发布我的主要课程。这段代码大部分取自我教授的示例程序。我的问题是我想在窗口按钮关闭后运行检查。“检查”将查看从另一个类“保存”的布尔变量是否为True或False,并相应地执行操作。然后,关上窗户。到目前为止,我有这个,但我从JFrameL得到一个错误。如何使用另一个类中的WindowClosing方法?我明白了,我不应该单独称呼它 import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; i

我有几节课,但我只发布我的主要课程。这段代码大部分取自我教授的示例程序。我的问题是我想在窗口按钮关闭后运行检查。“检查”将查看从另一个类“保存”的布尔变量是否为True或False,并相应地执行操作。然后,关上窗户。到目前为止,我有这个,但我从JFrameL得到一个错误。如何使用另一个类中的WindowClosing方法?我明白了,我不应该单独称呼它

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class Main {

public static JFrameL frame;

public static void main(String[] args) {
    // Create a window.
    frame  = new JFrameL("Checking Account Actions");

    // Set the size of the window.
    frame.setSize(450, 350);
    frame.setAlwaysOnTop(true);

    // Specify what happens when the close button is clicked.
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    CheckingAccountActions panel = new CheckingAccountActions();
    frame.getContentPane().add(panel);
    frame.pack();

    // Display the window
    frame.setVisible(true);

}

public class JFrameL extends JFrame {

    public JFrameL(String title) {
        super(title);
        FrameListener listener = new FrameListener();
        addWindowListener(listener);
    }

    private class FrameListener extends WindowAdapter {

        public void windowClosing(WindowEvent e) {
            System.out.println("WindowListener method called: windowClosed.");
            if(!CheckingAccountActions.saved) {
                String  message = "The data in the application is not saved.\n"
                                + "Would you like to save it before exiting the application?";
                int confirm = JOptionPane.showConfirmDialog (null, message);

                if (confirm == JOptionPane.YES_OPTION)
                    CheckingAccountActions.chooseFile(2);
            }
            Main.frame.setVisible(false);
            System.exit(0);
        }
    }
}

 }
我在第2行从main()接收到错误消息:

“无法访问Main类型的封闭实例。必须使用Main类型的封闭实例限定分配(例如x.new A(),其中x是Main的实例)。”

  • 您似乎试图以静态方式调用实例方法。不要这样做
  • 我不会为您正在尝试的操作扩展JFrame
  • 我将创建自己的WindowListener类,该类具有对对象的有效引用,该对象知道是否需要保存文件以及是否已保存文件
  • e、 g

    然后大体上:

    public static void main(String[] args) {
        frame  = new JFrame("Checking Account Actions");
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    
        CheckingAccountActions panel = new CheckingAccountActions();
    
        MyWindowAdapter winAdapter = new MyWindowAdapter(panel);
        frame.addWindowListener(winAdapter);
    
        frame.getContentPane().add(panel);
        frame.pack();
    
        frame.setVisible(true);
    }
    

    编辑


    这有一种静态过度使用的味道:
    检查Accountations.saved

    我想您需要做的是移动
    面板
    ,以便以后可以访问它

    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    
    
    public class Main {
    
    public static JFrameL frame;
    public static CheckingAccountActions panel;
    
    public static void main(String[] args) {
        // Create a window.
        frame  = new JFrameL("Checking Account Actions");
    
        // Set the size of the window.
        frame.setSize(450, 350);
        frame.setAlwaysOnTop(true);
    
        // Specify what happens when the close button is clicked.
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    
        panel = new CheckingAccountActions();
        frame.getContentPane().add(panel);
        frame.pack();
    
        // Display the window
        frame.setVisible(true);
    
    }
    
    public class JFrameL extends JFrame {
    
        public JFrameL(String title) {
            super(title);
            FrameListener listener = new FrameListener();
            addWindowListener(listener);
        }
    
        private class FrameListener extends WindowAdapter {
    
            public void windowClosing(WindowEvent e) {
                System.out.println("WindowListener method called: windowClosed.");
                if(!panel.saved) {
                    String  message = "The data in the application is not saved.\n"
                                    + "Would you like to save it before exiting the application?";
                    int confirm = JOptionPane.showConfirmDialog (null, message);
    
                    if (confirm == JOptionPane.YES_OPTION)
                        panel.chooseFile(2);
                }
                Main.frame.setVisible(false);
                System.exit(0);
            }
        }
    }
    

    可以那么这将取代我的程序的哪一部分呢?我把这个方法称为什么?因为CheckingAccountActions是一个所有活动都在其中进行的类。@EdoIsa:请参阅编辑。您似乎在以不应该的方式使用静态方法和字段,特别是如果save是静态变量,请不要这样做。另外,从WindowEvent参数获取JFrame。好的,谢谢!只是出于好奇,为什么我不能那样做?这是针对类btw的,如果安全性是个问题,我就不会制作程序。@EdoIsa:错误地使用静态,你就失去了OOP的所有优势。静态方法不只是让它在整个项目中都可以访问吗?对不起,这里还没人。就我所能理解的,我会指出这是其他类在某种程度上滥用静态方法的安全风险。你能简单地解释一下或者链接一下我可以在哪里读到更多关于静态方法误用的信息吗?我以前没听说过。
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    
    
    public class Main {
    
    public static JFrameL frame;
    public static CheckingAccountActions panel;
    
    public static void main(String[] args) {
        // Create a window.
        frame  = new JFrameL("Checking Account Actions");
    
        // Set the size of the window.
        frame.setSize(450, 350);
        frame.setAlwaysOnTop(true);
    
        // Specify what happens when the close button is clicked.
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    
        panel = new CheckingAccountActions();
        frame.getContentPane().add(panel);
        frame.pack();
    
        // Display the window
        frame.setVisible(true);
    
    }
    
    public class JFrameL extends JFrame {
    
        public JFrameL(String title) {
            super(title);
            FrameListener listener = new FrameListener();
            addWindowListener(listener);
        }
    
        private class FrameListener extends WindowAdapter {
    
            public void windowClosing(WindowEvent e) {
                System.out.println("WindowListener method called: windowClosed.");
                if(!panel.saved) {
                    String  message = "The data in the application is not saved.\n"
                                    + "Would you like to save it before exiting the application?";
                    int confirm = JOptionPane.showConfirmDialog (null, message);
    
                    if (confirm == JOptionPane.YES_OPTION)
                        panel.chooseFile(2);
                }
                Main.frame.setVisible(false);
                System.exit(0);
            }
        }
    }