Java 实现WindowListener错误

Java 实现WindowListener错误,java,swing,windowlistener,Java,Swing,Windowlistener,您好,我在向JFrame添加WindowListener时遇到问题。。。它说“windowClosing无法解析为类型”,我不知道如何修复错误 public Editor() { //Create JFrame For Editor JFrame SimplyHTMLJFrame = new JFrame(); SimplyHTMLJFrame.setTitle("Simply HTML - Editor"); SimplyHTMLJFrame.setSize(

您好,我在向JFrame添加WindowListener时遇到问题。。。它说“windowClosing无法解析为类型”,我不知道如何修复错误

public Editor() {
    //Create JFrame For Editor
    JFrame SimplyHTMLJFrame = new JFrame();

    SimplyHTMLJFrame.setTitle("Simply HTML - Editor");
    SimplyHTMLJFrame.setSize(800, 600);
    SimplyHTMLJFrame.setResizable(true);
    SimplyHTMLJFrame.setLocationRelativeTo(null);
    SimplyHTMLJFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    SimplyHTMLJFrame.addWindowListener(new windowClosing()); //The error is here it underlines windowClosing in red
    SimplyHTMLJFrame.setVisible(true);
    System.out.println("Editor - JFrame 'SimplyHTMLJFrame' - Created");

    //Program Closing Alert
    public void windowClosing(WindowEvent e) {
        int result = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit?\n"
                    + "All unsaved changes will be lost!","Confirm", JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
        if (result == JOptionPane.YES_OPTION) {
            System.exit(0);
        } else {
            //Do nothing
        }
    }
}

您所犯的错误是,您正在实例化一个方法而不是一个类型

SimplyHTMLJFrame.addWindowListener(new windowClosing());
这里的
windowClosing
JFrame
类中的一个方法

您需要创建自己的
WindowAdapter
/
WindowListener
,并将其作为侦听器添加到您的
JFrame

在相同/其他包中创建单独的类

class MyWindowAdapter extends WindowAdapter {

    @Override
    public void windowClosing(WindowEvent e) {
        int result = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit?\n"
                + "All unsaved changes will be lost!","Confirm", JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);

        if (result == JOptionPane.YES_OPTION) {

            System.exit(0);

        } else {

            //Do nothing

        }
    }


}
将其添加到您的
JFrame
编辑器中

SimplyHTMLJFrame.addWindowListener(new MyWindowAdapter()); 

您所犯的错误是,您正在实例化一个方法而不是一个类型

SimplyHTMLJFrame.addWindowListener(new windowClosing());
这里的
windowClosing
JFrame
类中的一个方法

您需要创建自己的
WindowAdapter
/
WindowListener
,并将其作为侦听器添加到您的
JFrame

在相同/其他包中创建单独的类

class MyWindowAdapter extends WindowAdapter {

    @Override
    public void windowClosing(WindowEvent e) {
        int result = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit?\n"
                + "All unsaved changes will be lost!","Confirm", JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);

        if (result == JOptionPane.YES_OPTION) {

            System.exit(0);

        } else {

            //Do nothing

        }
    }


}
将其添加到您的
JFrame
编辑器中

SimplyHTMLJFrame.addWindowListener(new MyWindowAdapter()); 

您必须为WindowListener回调实现一个内部类

public class Editor {

  public Editor() {

    // Create JFrame For Editor
    JFrame SimplyHTMLJFrame = new JFrame();

    SimplyHTMLJFrame.setTitle("Simply HTML - Editor");
    SimplyHTMLJFrame.setSize(800, 600);
    SimplyHTMLJFrame.setResizable(true);
    SimplyHTMLJFrame.setLocationRelativeTo(null);
    SimplyHTMLJFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    SimplyHTMLJFrame.addWindowListener(new WindowAdapter() {
      // Program Closing Alert
      public void windowClosing(WindowEvent e) {
        int result = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit?\n" + "All unsaved changes will be lost!", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);

        if (result == JOptionPane.YES_OPTION) {

          System.exit(0);

        } else {

          // Do nothing

        }
      }
    }); // The error is here it underlines windowClosing in red

    SimplyHTMLJFrame.setVisible(true);
    System.out.println("Editor - JFrame 'SimplyHTMLJFrame' - Created");

  }

您必须为WindowListener回调实现一个内部类

public class Editor {

  public Editor() {

    // Create JFrame For Editor
    JFrame SimplyHTMLJFrame = new JFrame();

    SimplyHTMLJFrame.setTitle("Simply HTML - Editor");
    SimplyHTMLJFrame.setSize(800, 600);
    SimplyHTMLJFrame.setResizable(true);
    SimplyHTMLJFrame.setLocationRelativeTo(null);
    SimplyHTMLJFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    SimplyHTMLJFrame.addWindowListener(new WindowAdapter() {
      // Program Closing Alert
      public void windowClosing(WindowEvent e) {
        int result = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit?\n" + "All unsaved changes will be lost!", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);

        if (result == JOptionPane.YES_OPTION) {

          System.exit(0);

        } else {

          // Do nothing

        }
      }
    }); // The error is here it underlines windowClosing in red

    SimplyHTMLJFrame.setVisible(true);
    System.out.println("Editor - JFrame 'SimplyHTMLJFrame' - Created");

  }
windowClosing()
是方法的名称,而不是可以实例化的类

您需要将WindowListener的实例传递给
SimplyHTMLJFrame.addWindowListener
。假设您的
编辑器
实现WindowListener
扩展WindowAdapter
,我基于
windowClosing
方法的存在所做的假设,这一行是有效的

SimplyHTMLJFrame.addWindowListener(this);
windowClosing()
是方法的名称,而不是可以实例化的类

您需要将WindowListener的实例传递给
SimplyHTMLJFrame.addWindowListener
。假设您的
编辑器
实现WindowListener
扩展WindowAdapter
,我基于
windowClosing
方法的存在所做的假设,这一行是有效的

SimplyHTMLJFrame.addWindowListener(this);
newwindowclosing()
不是一个类,因此不能实例化它。你有两个选择

  • 使类
    实现WindowListener
    并使用
    .addWindowListener(this)
  • 或者,创建一个注释性类

    SimplyHTMLJFrame.addWindowListener(new WindowAdapter(){
         @Override
         public void windowClosing(WindowEvent e) {
         ....
    });
    
注意,如果选择方法一,您将需要实现下面的所有窗口侦听器方法。您可以将不需要的方法保留为空方法,但它们仍然需要全部重写。如果选择第二种方法,则只需使用
WindowAdapter
,然后覆盖所需的方法

@Override
public void windowOpened(WindowEvent e) {}

@Override
public void windowClosing(WindowEvent e) {}

@Override
public void windowClosed(WindowEvent e) {}

@Override
public void windowIconified(WindowEvent e) {}

@Override
public void windowDeiconified(WindowEvent e) {}

@Override
public void windowActivated(WindowEvent e) {}

@Override
public void windowDeactivated(WindowEvent e) {}

作为旁注,最好对被重写的方法使用
@Override
注释,这样您就知道您正确地重写了一个方法。

new windowClosing()
不是一个类,所以您不能实例化它。你有两个选择

  • 使类
    实现WindowListener
    并使用
    .addWindowListener(this)
  • 或者,创建一个注释性类

    SimplyHTMLJFrame.addWindowListener(new WindowAdapter(){
         @Override
         public void windowClosing(WindowEvent e) {
         ....
    });
    
注意,如果选择方法一,您将需要实现下面的所有窗口侦听器方法。您可以将不需要的方法保留为空方法,但它们仍然需要全部重写。如果选择第二种方法,则只需使用
WindowAdapter
,然后覆盖所需的方法

@Override
public void windowOpened(WindowEvent e) {}

@Override
public void windowClosing(WindowEvent e) {}

@Override
public void windowClosed(WindowEvent e) {}

@Override
public void windowIconified(WindowEvent e) {}

@Override
public void windowDeiconified(WindowEvent e) {}

@Override
public void windowActivated(WindowEvent e) {}

@Override
public void windowDeactivated(WindowEvent e) {}


作为旁注,最好对被重写的方法使用
@Override
注释,这样您就知道您正确地重写了一个方法。

谢谢您回复得这么快,但我该怎么做呢?谢谢您回复得这么快,但是我该怎么做呢?谢谢你,我选择了你的答案,因为你是我用来解决问题的答案!谢谢,我选择了你的答案,因为你是我用来解决问题的答案!