您好,我在向我的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
}
}
}
最佳答案
您必须为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");
}
关于java - 实现WindowListener错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21380930/