سؤال

Hello I'm having a problem with adding a WindowListener to my JFrame... It's saying "windowClosing can't be resolved to a type" and I don't know how to fix the error.

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
        }
    }
}
هل كانت مفيدة؟

المحلول

You have to implement an inner class for the WindowListener callback.

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");

  }

نصائح أخرى

new windowClosing() is not a class, so you can't instantiate it. You have two options.

  • Make the class implements WindowListener and use .addWindowListener(this).
  • Or, create an annonymous class

    SimplyHTMLJFrame.addWindowListener(new WindowAdapter(){
         @Override
         public void windowClosing(WindowEvent e) {
         ....
    });
    

Note, if you choose method one, you will need to implements all the window listener methods below You can leave the ones you don't need, empty methods, but they still all need to be overridden. If you choose the second method, you can just use a WindowAdapter and just override the methods you need.

@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) {}

As a side note, it's good practice to use the @Override annotation for method that is being overriden, so you know that you are correctly overriding a method.

The mistake you have done is, you are instantiating a method instead of a type

SimplyHTMLJFrame.addWindowListener(new windowClosing());

here windowClosing is a method in your JFrame class

You need to create our own WindowAdapter/WindowListener and add it as listener to your JFrame

Create a separate class in same/other package

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

        }
    }


}

add it to your JFrame Editor

SimplyHTMLJFrame.addWindowListener(new MyWindowAdapter()); 

windowClosing() is the name of a method and not a class that can be instantiated.

You need to pass an instance of a WindowListener to SimplyHTMLJFrame.addWindowListener. Assuming that your Editor class either implements WindowListener or extends WindowAdapter, an assumption I'm making based on the presence of the windowClosing method, this line would be valid.

SimplyHTMLJFrame.addWindowListener(this);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top