Java - Message when closing JFrame Window

↘锁芯ラ 提交于 2019-11-29 07:34:53
Dan D.

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()); 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();
    }
  }
});
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.

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. Abstract classes cannot be instantiated. You would need to create a subclass of WindowAdapter and implement its abstract methods as public.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!