你好,我在给我的JFrame添加一个JFrame时遇到了问题.它的意思是"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
}
}
}发布于 2014-01-27 12:34:52
您必须为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");
}发布于 2014-01-27 12:35:24
new windowClosing()不是一个类,所以不能实例化它。你有两个选择。
implements WindowListener并使用.addWindowListener(this)。注意,如果选择方法一,则需要实现下面的所有窗口侦听器方法--您可以保留不需要的方法,即空方法,但它们仍然需要被重写。如果选择第二个方法,则只需使用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注释是很好的做法,因此您知道您正在正确地覆盖一个方法。
发布于 2014-01-27 12:34:19
您所犯的错误是,您正在实例化一个方法而不是一个类型。
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()); https://stackoverflow.com/questions/21380930
复制相似问题