Java 显示JFrame

Java 显示JFrame,java,swing,events,jframe,Java,Swing,Events,Jframe,当另一个窗口关闭时,如果另一个窗口是在子类中启动的,我在计算如何打开一个窗口时遇到问题。下面是我试图使用的笨拙代码,但它停止了子类窗口的可见设置。可能是因为它在一个动作事件中,或者可能是因为它停止了主线程 tutorial = new tutorialWindow(); this.setVisible(false); tutorial.setLocationRelativeTo(null); tutorial.setVisible(true); tutor

当另一个窗口关闭时,如果另一个窗口是在子类中启动的,我在计算如何打开一个窗口时遇到问题。下面是我试图使用的笨拙代码,但它停止了子类窗口的可见设置。可能是因为它在一个动作事件中,或者可能是因为它停止了主线程

    tutorial = new tutorialWindow();
    this.setVisible(false);
    tutorial.setLocationRelativeTo(null);
    tutorial.setVisible(true);
    tutorial.setCurrentUser(users.getCurrentUser());

    while(tutorial.isOpen() == true ) {
    }
    this.setVisible(true);
    users.updateUser(tutorial.getCurrentUser());
我的想法是,它只会卡在代码部分,直到另一个窗口关闭,然后当tutorialWindow的Open boolean设置为false时,它会再次出现,因为它打破了while循环

我确信这是一个使用正确线程的问题,或者可能是各种notify方法的问题,但到目前为止,我不确定如何做到这一点。

您可以使用。在下面的示例中,实现了
WindowListener
,我只是覆盖了
public void windowClosed(final WindowEvent e)
方法,打开了第二个窗口

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class TestJFrame {

    public static void main(final String args[]) {
        JFrame jFrame1 = new JFrame();
        jFrame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jFrame1.add(new JLabel("First JFrame"));
        jFrame1.pack();

        final JFrame jFrame2 = new JFrame();
        jFrame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jFrame2.add(new JLabel("Second JFrame"));
        jFrame2.pack();

        jFrame1.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(final WindowEvent e) {
                jFrame2.setVisible(true);
            }
        });

        jFrame1.setVisible(true);

    }

}

1) 为了更好的帮助,请尽快发布一篇文章,2)不要使用多个窗口。我之所以使用空布局是因为各种原因。@LiamB:在任何情况下都不需要绝对定位。为了让某人对您的查询给出一个好的答案,请发布一个@nIcE cOw:Will do,即使编写一个小类来请求使用while循环来管理JFrames,也几乎不值得。此项目需要绝对位置,在Netbeans IDE中使用大量动画和重叠的JLabel,它们在没有空布局的情况下不会格式化。@LiamB:但是在GUI中使用
while(true)
循环可能会停止EDT事件调度线程。Francisco的答案似乎是正确的:-)+1对于这个答案,我认为这会起作用,问题是我有用jLabels制作的自定义关闭按钮来运行这个。dispose();我现在如何集成它?哦,对不起,这是我的错:我实现了
windowClosing
,但右边是
windowClosing
,只要更改它,您就会看到它会工作。嗯,现在我想起来,这不起作用。如果两个JFrame都在同一个类中声明,那么它会起作用,但是由于我关闭的JFrame是在主JFrame中声明的类中声明的,因此我无法从教程窗口中引用主JFrame的可见集(在类heirachy中声明为lower down).我想现在我会使主框架不关闭,但会隐藏在背景中,所以我不需要重新打开它,它会解决问题。但我仍然想知道如何正确地回避这个问题。