confirmation before press YES to exit program in Java

后端 未结 3 942
悲哀的现实
悲哀的现实 2021-02-11 04:17
private void windowClosing(java.awt.event.WindowEvent evt)                               
    {                                   
        int confirmed = JOptionPane.sh         


        
3条回答
  •  余生分开走
    2021-02-11 04:39

    From what i understand you want something like this

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

    If you want to use it on some button do similiar function to button. Put listener on it and do same. But I'm not sure if I get your question right. But If you want to use button use ActionListener and action performed method.

    check question - Java - Message when closing JFrame Window

提交回复
热议问题