且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在不等待用户的 Gui 输入的情况下阻止 Java 运行整个代码

更新时间:2022-03-10 23:08:45

你不应该让一个 JFrame 启动其他 JFrame,尤其是如果你希望子窗口表现得像一个模态对话框——一个暂停代码的对话框启动窗口,直到它被完全处理.在这种情况下,通过使用模态 JDialogs 代替对话框窗口的 JFrames 来制作对话框窗口 dialogs.

You shouldn't have a JFrame launching other JFrames, especially if you want the child windows to behave as a modal dialogs -- a dialog that halts the code in the launching window until it has been fully dealt with. When this is the case, make the dialog windows dialogs by using modal JDialogs in place of JFrames for the dialog windows.

例如:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MainWelcomeGui2 {
   public static void main(String[] args) {
      final JFrame frame = new JFrame("Main GUI");

      JButton addDeptButtonLaunchJFrame = new JButton(
            "Add a New Department, Launch JFrame");
      JButton addDeptButtonLaunchJDialog = new JButton(
            "Add a New Department, Launch JDialog");

      addDeptButtonLaunchJDialog.addActionListener(new LaunchJDialogListener(
            frame));
      addDeptButtonLaunchJFrame.addActionListener(new LaunchJFrameListener());

      JPanel panel = new JPanel();
      panel.add(addDeptButtonLaunchJDialog);
      panel.add(addDeptButtonLaunchJFrame);

      frame.add(panel);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);

   }
}

class LaunchJDialogListener implements ActionListener {
   JDialog dialog;

   public LaunchJDialogListener(JFrame parentFrame) {
      JButton doneButton = new JButton(new AbstractAction("Done") {
         public void actionPerformed(ActionEvent e) {
            dialog.dispose();
         }
      });

      JPanel panel = new JPanel();
      panel.setPreferredSize(new Dimension(100, 100));
      panel.add(doneButton);

      dialog = new JDialog(parentFrame, "Dialog", true);
      dialog.add(panel);
      dialog.pack();
      dialog.setLocationRelativeTo(null);
   }

   @Override
   public void actionPerformed(ActionEvent e) {
      System.out.println("called before setting dialog visible");
      dialog.setVisible(true);
      System.out
            .println("called after setting dialog visible. Note that this line doesn't show until the dialog disappears");
   }
}

class LaunchJFrameListener implements ActionListener {
   JFrame frame;

   public LaunchJFrameListener() {
      JButton doneButton = new JButton(new AbstractAction("Done") {
         public void actionPerformed(ActionEvent e) {
            frame.dispose();
         }
      });

      JPanel panel = new JPanel();
      panel.setPreferredSize(new Dimension(100, 100));
      panel.add(doneButton);

      frame = new JFrame("JFrame");
      frame.add(panel);
      frame.pack();
      frame.setLocationRelativeTo(null);
   }

   @Override
   public void actionPerformed(ActionEvent e) {
      System.out.println("called before setting frame visible");
      frame.setVisible(true);
      System.out
            .println("called after setting frame visible.  Note that this line shows up immediately.");
   }
}