[英]Java - Message when closing JFrame Window
I have a Java Program containing a class Application inheriting from JFrame. 我有一个Java程序,其中包含一个继承自JFrame的类Application 。
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. 我自己编写了WindowClosing事件处理程序。 However, I have trouble registering the window listener (addWindowListener). 但是,我在注册窗口侦听器(addWindowListener)时遇到问题。 It is telling me that WindowAdapter is abstract and cannot be instantiated. 它告诉我WindowAdapter是抽象的,无法实例化。
How can I solve this problem please? 我该如何解决这个问题?
Basically, you got it almost correct. 基本上,你几乎是正确的。 There are a few things not put together correctly and a typo. 有些事情没有正确拼凑而成。
First remove your WindowClosing
method (it's window
, not Window
) Then replace your addWindowListener(new WindowAdapter());
首先删除你的WindowClosing
方法(它的window
,而不是Window
)然后替换你的addWindowListener(new WindowAdapter());
with the code below 使用下面的代码
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();
}
}
});
i got this in two minutes coding.... 我在两分钟内得到了这个......
First is set the j frame default closing event in Exit_on_close. 首先在Exit_on_close中设置j帧默认关闭事件。 Second create a class called "Window Closing Event Handler" and then call it in the i nit stage. 其次创建一个名为“Window Closing Event Handler”的类,然后在i nit阶段调用它。
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);
}
}
});
}
Ok trying again. 好的再试一次。
You cannot create a new WindowAdapter because WindowAdapter is abstract. 您无法创建新的WindowAdapter,因为WindowAdapter是抽象的。 Abstract classes cannot be instantiated. 抽象类无法实例化。 You would need to create a subclass of WindowAdapter and implement its abstract methods as public. 您需要创建WindowAdapter的子类并将其抽象方法实现为public。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.