如果另一个窗口是在子类中启动的话,我在解决如何打开一个窗口时遇到问题。这是我试图使用的笨拙的代码,但它停止了sub classe窗口的可见设置。也许是因为它在一个动作事件中,或者它正在停止主线程。
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布尔值设置为false时再次出现,因为它打破了while循环。
我确定这是使用正确的线程,或者可能是各种通知方法,但截至目前我不知道该怎么做。
答案 0 :(得分:4)
你可以使用WindowListener
来完成。在以下示例WindowAdapter
中实现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);
}
}