- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,我在向 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/
如果您有一个 WindowListener,那么当窗口关闭时 windowDeactivated(WindowEvent) 事件是否总是发生,或者 windowClosing(WindowEvent
我有一个服务器/客户端应用程序。系统启动后,会弹出登录窗口。单击“连接”时,会弹出另一个 JFrame(游戏框架)。我想要的是:当单击游戏框架上的“X”时 - 当前用户的游戏将停止,登录窗口将再次弹出
我希望我的 GUI 在出现 JOptionPane 时进行一些检查。因为我找不到任何其他方法,所以我可以在每次应用程序窗口失去焦点时执行这些操作(它只是检查一个字符串)。为此,我在我的 JFrame
我正在编写一个 GUI。我想在关闭后打印一些东西。但是 WindowListener 不起作用。我写了一个窗口,然后我想要一个 boolean 值来标记窗口已关闭。因此,我可以在 if 子句中使用该
您好,我在向 JFrame 添加 WindowListener 时遇到问题...它说“windowClosing 无法解析为类型”,但我不知道如何修复该错误。 public Editor() {
这是我开发的代码。这是保存并执行我的游戏的每个外部 JFrame 的主程序。 ChooseGender 是一个外部程序,只不过是一个 JFrame 及其组件。 我的目标是当chooseGender执行
我正在根据 CodeReview.SE 问题的建议重构我的代码。 https://codereview.stackexchange.com/questions/126634/button-clicki
我试图通过使用 Action 对象将我的 GUI 应用程序中的功能与状态分开。我已经成功地使用它们来创建具有相同功能的菜单项和按钮。 我的问题是:我想对菜单中的“退出”项和框架的关闭按钮执行相同的操作
我有一个带有抽象类的应用程序,它扩展了 JDialog。该类作为一个abstract void onClose(),并且在该类的构造函数中,添加了以下代码: addWindowListener(new
这个问题已经有答案了: Best practice for setting JFrame locations (4 个回答) 已关闭 6 年前。 这个梦幻足球计划需要一些帮助,正在努力弄清楚如何实现我
我正在努力使用 WindowListener 来关闭 JFrame。 我遇到一种情况,客户端登录到服务器,当客户端关闭其应用程序时,需要通知服务器。因此,为了通知服务器,应该处理类的另一个实例(处理
我在 Swing 演示应用程序中看到以下代码: WindowListener wndCloser = new WindowAdapter() { public void windowClosi
我正在尝试分离我的一些代码,以便我可以为不同的项目提供一个可重用的类。我现在拥有的类称为 MainFrame,它现在正在做的就是创建一个带有 JMenuBar 的窗口,该窗口具有一个 JMenu 文件
是否有一个 Java WindowListener 在显示窗口之前被调用? 我已经尝试了 windowOpened (下面的示例)和 componentShown。这两个函数都会在窗口显示后被调用。是
mainFrame.addWindowListener(new WindowListener() { @Override public void windowClosi
我有一个用 AWT 编写的程序,所以我使用 Frame(不是 JFrame/Swing)。我正在使用 MenuItem 对象通过 ActionListener 执行一些操作。 但是,在我的最后一个 M
这是我的 View 类。主类实际上启动了程序。下面的代码在 View 类中使用,因为它包含 GUI 和事件处理程序( Action 监听器)。 public class TheaterView ext
我使用的是Jide对接框架中的DockableFrame类(扩展了JComponent)。 我想添加一个监听器以在对接框架关闭时运行一些代码。 我尝试查找信息,但我读到的每个资料都告诉我使用 addW
我有一个框架,想在用户关闭它时提示用户保存文档。但如果他们取消,则框架不应关闭。 frame.addWindowListener(new SaveOnCloseWindowListener(fileS
UPD:解决了! 美好的一天! 我开始在 netbeans 6.9.1 中开发 JAVA GUI 应用程序(我使用 matisse)。所以我决定在我的程序中实现 windowListener 但我遇到
我是一名优秀的程序员,十分优秀!