关闭窗口后如何自动保存TextArea中的内容?

Cof*_*fee 1 java swing static compiler-errors

我有一个Java GUI程序,它模拟一个简单的文本字段(就像粘滞便笺).我希望能够单击关闭按钮,它会自动将其保存到目录中的预定义文本字段.

但我遇到静态错误的问题:

File: C:\Users\Adel\Code\Javas\popupText.java  [line: 538]
Error: non-static method open() cannot be referenced from a static context
Run Code Online (Sandbox Code Playgroud)

这是我的程序的主要方法:

 public static void main(String args[])
  {
    popupText note = new popupText("Untitled-Notepad");
    note.setSize(600,600);
    note.setLocation(200,200);
    note.setVisible(true);

    WindowListener exitListener = new WindowAdapter() {

      @Override
      public void windowClosing(WindowEvent e) {
        int confirm = JOptionPane.showOptionDialog(null, "Are You Sure to Close Application?", "Exit Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
        if (confirm == 0) {

          open();   //THIS CAUSES ERROR!!
          System.out.print("Yay I openeed");
          addToStatic();  //just added @ end
          System.exit(0);
        }
      }
    };
    text.append(guts);

    note.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
    note.addWindowListener(exitListener);
Run Code Online (Sandbox Code Playgroud)

文本区域很简单,看起来像这样,如果你愿意,我也可以提供完整的代码(目前它只是有点未格式化):

在此输入图像描述

Mad*_*mer 5

mainstatic,但open方法不是.

您只能static从其他static方法访问方法(和字段).

没有进一步的代码,我建议你尝试相似的东西note.open()(假设open是一种注意的方法)