2014-01-27 66 views
-1

你好我有一個WindowListener添加到我的JFrame的問題......它說「windowClosing不能解析爲一個類型」,我不知道如何解決這個錯誤。實現WindowListener錯誤

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 
     } 
    } 
} 

回答

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

    } 
+0

謝謝我已經選擇了你的答案,因爲你是我用來解決問題的答案! – 2602

1

你所做的錯誤是,你實例化一個方法,而不是一個類型

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 

     } 
    } 


} 

它添加到您的JFrameEditor

SimplyHTMLJFrame.addWindowListener(new MyWindowAdapter()); 
+0

謝謝你這麼快回復,但我該怎麼去做呢? – 2602

0

windowClosing()是一個方法的名稱,而不是可以實例化的類。

您需要將WindowListener的實例傳遞給SimplyHTMLJFrame.addWindowListener。假設你的Editor類要麼是implements WindowListener要麼是extends WindowAdapter,這個假設我是基於windowClosing方法的存在而做出的,這條線是有效的。

SimplyHTMLJFrame.addWindowListener(this); 
2

new windowClosing()是不是一類,所以你不能實例化。你有兩個選擇。

  • 使類implements WindowListener和使用.addWindowListener(this)
  • 或者,創建一個類annonymous

    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標註爲正在重寫方法很好的做法,所以你知道你是正確的覆蓋方法。