【问题标题】:How to close multiple JFrame and JDialog windows?如何关闭多个 JFrame 和 JDialog 窗口?
【发布时间】:2015-01-18 10:24:30
【问题描述】:

我正在开发一个具有多个 JFrameJDialog 窗口的程序。

我有一个包含一个按钮的 JFrame,当我点击这个按钮时,会打开一个 JDialog 窗口。在这个 JDialog 窗口中还有另一个按钮,单击它会打开第二个 JDialog 窗口。在第二个 JDialog 窗口中,我有一个最后一个按钮。

我想要做的是在单击最后一个按钮时关闭JDialog 窗口和JFrame 窗口。

开盘顺序是这样的:

JFrame Frame1;
JButton Button1;

JDialog Dialog1;
JButton Button2;

JDialog Dialog2;
JButton Button3;

Button1ActionPerformed(ActionEvent e){
   new Dialog(Frame1Frame);
}

Button2ActionPerformed(ActionEvent e){
    new Dialog2(Dialog1Frame)
}

Button3ActionPerformed(ActionEvent e){
   //Here I wnat to add the code that closes JDialog2 JDialog1 and JFrame1 windows.
}

我尝试过super.dispose();,但它不起作用。有什么想法吗?

【问题讨论】:

  • 你实现了 ActionListener 吗?
  • 是的,我做到了。任何的想法?如果您想查看实际代码,我可以发布它。
  • 您需要参考其他窗口才能关闭它们。
  • 我认为@MadProgrammer 将要说的是,如果您获得了要关闭的实际对话框实例(使用调试检查)

标签: java swing jframe jdialog


【解决方案1】:

如图所示here 使用Action,您的actionPerformed() 实现可以将WINDOW_CLOSING 事件分派到所需的Window 实例。

@Override
public void actionPerformed(ActionEvent e) {
    d1.dispatchEvent(new WindowEvent(d1, WindowEvent.WINDOW_CLOSING));
    d2.dispatchEvent(new WindowEvent(d2, WindowEvent.WINDOW_CLOSING));
    f1.dispatchEvent(new WindowEvent(f1, WindowEvent.WINDOW_CLOSING));
}

【讨论】:

    【解决方案2】:

    可能有更好的方法可以做到这一点,但这里有一种通用的方法可能会有所帮助。

    在您的代码中,您创建了窗口,但没有将对您创建的窗口的引用存储到变量中。例如,您有:

    JDialog Dialog1;
    

    然后,当您创建 Dialog1 的实例时,您有以下代码:

    Button1ActionPerformed(ActionEvent e){
        new Dialog(Frame1Frame);
    }
    

    这意味着您已经创建了 Dialog,但您没有保留对 Dialog 的引用以供您的代码稍后操作。如果您在此处分配此值,您应该可以稍后对其进行操作。

    如果您将实现更改为:

    Button1ActionPerformed(ActionEvent e){
        Dialog1 = new Dialog(Frame1Frame);
    }
    

    随后在您的代码中,您将引用 Dialog 以便对其进行操作,

    Button3ActionPerformed(ActionEvent e){
       Dialog1.dispose();
       // you can manipulate the variables of the class from here and close other windows etc.
    }
    

    【讨论】:

      【解决方案3】:

      如果你有对象引用,你可以这样做:

      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import javax.swing.JButton;
      import javax.swing.JDialog;
      import javax.swing.JFrame;
      
      
      public class Main
      {
          private static JFrame frame;
      
          private static JButton buttonFrame;
      
      
          private static JDialog dialog1;
      
          private static JButton buttonDialog1;
      
      
          private static JDialog dialog2;
      
          private static JButton buttonDialog2;
      
      
          public static void main(String[] args) {
      
              /* frame */
      
              frame = new JFrame("Main Frame");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setSize(400, 400);
              frame.setLocationRelativeTo(null);
      
              buttonFrame = new JButton("open dialog 1");
              buttonFrame.addActionListener(new ActionListener() {
                  @Override public void actionPerformed(ActionEvent e) {
                      dialog1.setVisible(true);
                  }
              });
      
              frame.add(buttonFrame);
      
              /* dialog 1 */
      
              dialog1 = new JDialog(frame, "Dialog 1");
              dialog1.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
              dialog1.setSize(300, 300);
              dialog1.setLocationRelativeTo(null);
      
              buttonDialog1 = new JButton("open dialog 2");
              buttonDialog1.addActionListener(new ActionListener() {
                  @Override public void actionPerformed(ActionEvent e) {
                      dialog2.setVisible(true);
                  }
              });
      
              dialog1.add(buttonDialog1);
      
              /* dialog 2 */
      
              dialog2 = new JDialog(dialog1, "Dialog 2");
              dialog2.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
              dialog2.setSize(200, 200);
              dialog2.setLocationRelativeTo(null);
      
              buttonDialog2 = new JButton("close all");
              buttonDialog2.addActionListener(new ActionListener() {
                  @Override public void actionPerformed(ActionEvent e) {
                      dialog2.dispose();
                      dialog1.dispose();
                      frame.dispose();
                  }
              });
      
              dialog2.add(buttonDialog2);
      
              /* show frame */
      
              frame.setVisible(true);
          }
      }
      

      否则你可以使用System.exit(0);:

      buttonDialog2.addActionListener(new ActionListener() {
          @Override public void actionPerformed(ActionEvent e) {
              System.exit(0);
          }
      });
      

      【讨论】:

      • 我厌倦了使用引用,但它不起作用。这是我的代码。 while (resultSet.next()) { if (SessionID == resultSet.getInt(1)) { EditProfileInterface.Dialog2.dispose(); ProfileInterface.Dialog1.dispose(); DeleteAccountInterface.dispose();新的登录界面();休息;有什么问题吗?/
      • 刚刚编辑。试试新代码,它会更好地向您展示如何使用 dispose() 方法。它有效。
      【解决方案4】:

      通过另一个JFrame关闭多个JFrame-

      可以关闭多个窗口,甚至是非静态 Java 摆动窗口。输入下面的小代码,一步一步来。

      所有主要方法将仅输入 Class1。

      在第一类中创建第二类全局变量。

      `Class2 NiceApp1;

      创建两个文本字段 jTextField1 和 jTextField2 并在 jTextField1 = 1, jTextField2 = 0 中赋值。

      您也可以在文本字段中输入您的窗口名称,然后将其与您的窗口名称进行比较,而不使用浮点数。

      //按钮名称为jToggleButton1。您需要顺时针单击按钮并选择执行的操作,然后键入以打开第二个类。

      private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {

          NiceApp1 = new Class2();
          NiceApp1.setVisible(true);
          jTextField1.setText("0");
      

      }

      现在在 Class1 中键入以下方法。

          public void NiceApplication1() throws NullPointerException {
      
          if (NiceApp1.isVisible() == true) {
      
              jTextField1.setText("1.1");
      
          } else {
              jTextField1.setText("0");
      
          }
      
          if (jTextField1.getText().equals(jTextField2.getText())) {
      
              Close();
      
          }
      
      }
      

      //现在你需要使用Focus Gained,点击同样的jToggleButton1选择FocusGained。

          private void jToggleButton1FocusGained(java.awt.event.FocusEvent evt) {                                           
          
          NiceApplication1();
      
      }                                          
      

      //现在只需要确定您使用任何 JLabel 或仅 class1 中的面板,如果您使用覆盖屏幕的 JLabel,因此顺时针单击并选择 JLabel MouseEntered 或者您使用面板,因此使用面板的 MouseEntered,下面的示例使用 JLabel。

          private void jLabelMouseEntered(java.awt.event.MouseEvent evt) {                                     
          
          
         NiceApplication1();
          
      }                                    
      

      //现在只输入你的类关闭方法。

      public void Close() {
          WindowEvent winclosing = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
          Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winclosing);
      
      }
      

      //在Class2中,键入close方法,然后在关闭后打开你想要打开的类。像下面的例子那样输入。

          Close();
          Class NiceApp1 = new Class();
          NiceApp1.setVisible(true);
          
      

      //在Class2 Cancel按钮类型打开Class1以防万一,用户取消所以之前的Class1会关闭,新的Class1会重新打开。

          Close();
          Class1 NiceApp1 = new Class1();
          NiceApp1.setVisible(true);
      

      `

      注意,如果用户将打开任何班级,那么在用户尝试将光标移动到 Class1 上之后,之前的 Class1 屏幕仍然会关闭。如果你想同时关闭 class1,那么使用 Class2 的焦点损失来打开新的任何类,并在执行的操作中调用 close 方法,如果有弹出窗口,那么在 yes-no 弹出窗口之外调用 close 方法。

      如果你做了上述过程,你现在已经完成了。

      如果您无法顺时针单击按钮来选择执行的操作等,请使用 jToggleButton1.addActionListener。

      您也可以访问 NiceApplication1.BlogSpot.Com。

      您可以使用上面的代码。从 Class2 关闭窗口,您将看到之前的窗口也将关闭,并且将显示一个新的新班级窗口。

      【讨论】:

        猜你喜欢
        • 2011-12-21
        • 1970-01-01
        • 2013-05-21
        • 1970-01-01
        • 2022-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多