如何阻止方法启动,直到 JFrame 关闭



我同时使用JFrame和JOption窗格,我的main方法的开头看起来像这样:

public static void main(String[] args) {
   welcomeScreen();
   int input = getInput();

其中welcomeScreen 调用扩展 JFrame 的对象,getInput 使用 JOptionPane,如下所示:

String s = "blah blah"
String data = JOptionPane.showInputDialog(s);
return data;

当你运行程序时,JOption窗格和JFrame同时打开,但我只希望打开在main方法中首先调用的JFrame,我希望JOptionPane在我通过JButton关闭JFrame后打开,实现actionListener:

public class close implements ActionListener {
    public void actionPerformed(ActionEvent aL) {
      System.exit(0);
    }
  }

我该怎么做?

您可以将 WindowListener 添加到框架中,因此当它检测到关闭框架的操作时,它将运行您在重写的 windowClosing() 方法中实现的代码。你可以做这样的事情:

frame.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {
        frame.dispose();
        JOptionPane.showInputDialog("your message");
    }
});

最新更新