你好我在向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
}
}
}
答案 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());
此处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
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 WindowListener
或extends WindowAdapter
,我根据windowClosing
方法的存在做出假设,此行有效。
SimplyHTMLJFrame.addWindowListener(this);