gpt4 book ai didi

java - 实现 WindowListener 错误

转载 作者:行者123 更新时间:2023-12-01 18:37:21 25 4
gpt4 key购买 nike

您好,我在向 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com