Java - 关闭 JFrame 窗口时的消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13419947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:39:04  来源:igfitidea点击:

Java - Message when closing JFrame Window

javaswingwindowjframe

提问by Matthew

I have a Java Program containing a class Applicationinheriting from JFrame.

我有一个 Java 程序,其中包含一个从 JFrame 继承的类应用程序

I want to display a message which asks the user if he wants to exit the program upon clicking the X button at the top right of the window.

我想显示一条消息,询问用户是否要在单击窗口右上角的 X 按钮后退出程序。

This is my code so far:

到目前为止,这是我的代码:

I got this code from a tutorial I found online. I coded the WindowClosing event handler myself. However, I have trouble registering the window listener (addWindowListener). It is telling me that WindowAdapter is abstract and cannot be instantiated.

我从网上找到的教程中得到了这段代码。我自己编写了 WindowClosing 事件处理程序。但是,我无法注册窗口侦听器 (addWindowListener)。它告诉我 WindowAdapter 是抽象的,无法实例化。

How can I solve this problem please?

请问我该如何解决这个问题?

回答by Dan D.

Basically, you got it almost correct. There are a few things not put together correctly and a typo.

基本上,你几乎是正确的。有一些东西没有正确组合在一起,还有一个错字。

First remove your WindowClosingmethod (it's window, not Window) Then replace your addWindowListener(new WindowAdapter());with the code below

首先删除你的WindowClosing方法(它是window,不是Window)然后用addWindowListener(new WindowAdapter());下面的代码替换你的

addWindowListener(new WindowAdapter() {
  public void windowClosing(WindowEvent e) {
    int confirmed = JOptionPane.showConfirmDialog(null, 
        "Are you sure you want to exit the program?", "Exit Program Message Box",
        JOptionPane.YES_NO_OPTION);

    if (confirmed == JOptionPane.YES_OPTION) {
      dispose();
    }
  }
});

回答by Synester

i got this in two minutes coding....

我在两分钟内得到了这个编码....

First is set the j frame default closing event in Exit_on_close. Second create a class called "Window Closing Event Handler" and then call it in the i nit stage.

首先是在 Exit_on_close 中设置 j 帧默认关闭事件。其次创建一个名为“窗口关闭事件处理程序”的类,然后在初始化阶段调用它。

private void WindowClosingEventHandler(){ addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { int confirmed = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit this application?", "Exit Program Message Box",JOptionPane.YES_NO_OPTION);

    if (confirmed == JOptionPane.YES_OPTION) {
        try{
            String login=txtuserid.getText();
            Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/repair", "root", "");
            Statement st = conn.createStatement();
            String update = "UPDATE user set User_Status=0 where UserID='"+ login +"'";
            st.executeUpdate(update);  
            dispose();
            Login2 dialog = new Login2(new javax.swing.JFrame(), true);
            dialog.setVisible(true);
        }catch(SQLException | HeadlessException q){
            JOptionPane.showMessageDialog(null, q);
        }
        System.exit(0);
    }
    else{
        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
    }
}
});
}

回答by Scuba Steve

Ok trying again.

好的,再试一次。

You cannot create a new WindowAdapterbecause WindowAdapter is abstract. Abstract classes cannot be instantiated. You would need to create a subclass of WindowAdapter and implement its abstract methods as public.

您不能创建新的 WindowAdapter,因为 WindowAdapter 是抽象的。抽象类不能被实例化。您需要创建 WindowAdapter 的子类并将其抽象方法实现为公共。

http://docs.oracle.com/javase/7/docs/api/java/awt/event/WindowAdapter.html

http://docs.oracle.com/javase/7/docs/api/java/awt/event/WindowAdapter.html