无法将JOptionPane设置为不可见,然后再次返回可见状态

时间:2012-09-14 06:04:18

标签: java swing concurrency joptionpane event-dispatch-thread

我想要实现的是

  • 在某种状态下,类应自动隐藏其他类显示的对话框
  • 当程序状态发生变化时,应显示隐藏的对话框

问题:

  • 这不适用于JOptionPanes
  • JOptionPanes被隐藏并再次显示,但随后它们会自动关闭,所以我只会在一秒钟内再次看到它们

我使用了以下方法:

public static void main(String[] args) {


    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {

        public void eventDispatched(AWTEvent event) {
            WindowEvent windowEvent = ((WindowEvent) event);
            System.out.println(System.currentTimeMillis() + " " + windowEvent);
            switch (windowEvent.getID()) {
            case WindowEvent.WINDOW_OPENED:
                System.out.println("Hiding");
                windowEvent.getComponent().setVisible(false);
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Showing");
                windowEvent.getComponent().setVisible(true);
                break;
            }
        }


    }, AWTEvent.WINDOW_EVENT_MASK + AWTEvent.WINDOW_STATE_EVENT_MASK);

    JOptionPane.showMessageDialog(null,
            "Eggs are not supposed to be green.",
            "Inane custom dialog",
            JOptionPane.INFORMATION_MESSAGE);
}

它产生以下输出:

1347602481337 java.awt.event.WindowEvent[WINDOW_ACTIVATED,opposite=null,oldState=0,newState=0] 
on dialog0
1347602481337 java.awt.event.WindowEvent[WINDOW_GAINED_FOCUS,opposite=null,oldState=0,newState=0] on dialog0
1347602481337 java.awt.event.WindowEvent[WINDOW_OPENED,opposite=null,oldState=0,newState=0] on dialog0
Hiding
Showing
1347602486377 java.awt.event.WindowEvent[WINDOW_LOST_FOCUS,opposite=null,oldState=0,newState=0] on dialog0
1347602486377 java.awt.event.WindowEvent[WINDOW_DEACTIVATED,opposite=null,oldState=0,newState=0] on dialog0
1347602486377 java.awt.event.WindowEvent[WINDOW_ACTIVATED,opposite=null,oldState=0,newState=0] on dialog0
1347602486377 java.awt.event.WindowEvent[WINDOW_GAINED_FOCUS,opposite=null,oldState=0,newState=0] on dialog0
1347602486377 java.awt.event.WindowEvent[WINDOW_LOST_FOCUS,opposite=null,oldState=0,newState=0] on dialog0
1347602486377 java.awt.event.WindowEvent[WINDOW_DEACTIVATED,opposite=null,oldState=0,newState=0] on dialog0
1347602486377 java.awt.event.WindowEvent[WINDOW_CLOSED,opposite=null,oldState=0,newState=0] on dialog0
1347602486377 java.awt.event.WindowEvent[WINDOW_CLOSED,opposite=null,oldState=0,newState=0] on dialog0
1347602486377 java.awt.event.WindowEvent[WINDOW_CLOSED,opposite=null,oldState=0,newState=0] on frame0

我的问题是,我做错了什么?这是每个设计还是我犯了错误,我是否以错误的方式使用这些类?如果是,那么正确的方法是什么?

1 个答案:

答案 0 :(得分:3)

你做错了就是在Event Dispatch Thread上休息:

windowEvent.getComponent().setVisible(false);
try {
  Thread.sleep(5000);
} catch (InterruptedException e) {
  e.printStackTrace();
}
System.out.println("Showing");
windowEvent.getComponent().setVisible(true);

通过阻止EDT 5秒钟,没有任何东西可以重新粉刷。请改用Timer

有关详细信息,请参阅Concurrency in Swing教程。