实现WindowListener错误

时间:2014-01-27 12:28:45

标签: java swing windowlistener

你好我在向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
        }
    }
}

4 个答案:

答案 0 :(得分:2)

new windowClosing()不是类,因此您无法实例化它。你有两个选择。

  • 制作课程implements 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注释,因此您知道正确覆盖了方法。

答案 1 :(得分:1)

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

SimplyHTMLJFrame.addWindowListener(new windowClosing());

此处windowClosingJFrame

中的一种方法

您需要创建我们自己的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 Editor

SimplyHTMLJFrame.addWindowListener(new MyWindowAdapter()); 

答案 2 :(得分:1)

您必须为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");

  }

答案 3 :(得分:0)

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

您需要将WindowListener的实例传递给SimplyHTMLJFrame.addWindowListener。假设您的Editor课程为implements WindowListenerextends WindowAdapter,我根据windowClosing方法的存在做出假设,此行有效。

SimplyHTMLJFrame.addWindowListener(this);