在按 YES 退出 Java 程序之前确认

2022-01-24 00:00:00 window java swing jframe joptionpane
private void windowClosing(java.awt.event.WindowEvent evt)                               
    {                                   
        int confirmed = JOptionPane.showConfirmDialog(null, "Exit Program?","EXIT",JOptionPane.YES_NO_OPTION);
        if(confirmed == JOptionPane.YES_OPTION)
        {
            dispose();
        }
    }

我想通过按下关闭窗口按钮并确认来关闭程序...但是当我选择否"返回我的 Jframe 时,它​​仍然可以帮助我退出程序???

I want to close program by pressing Close Window Button with confirmation...But when I choose "No" to back to my Jframe, it still helps me to exit the program???

推荐答案

据我了解你想要这样的东西

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

如果您想在某个按钮上使用它,请执行与按钮类似的功能.将听众放在上面并做同样的事情.但我不确定你的问题是否正确.但是如果你想使用按钮使用 ActionListener 和 action executed 方法.

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.

检查问题 - Java - 关闭 JFrame 窗口时的消息

相关文章