How to stop Java from running the entire code with out waiting for Gui input from The user

倖福魔咒の 提交于 2019-11-28 02:16:49

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.

For example:

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