我想要实现的是
问题:
我使用了以下方法:
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
我的问题是,我做错了什么?这是每个设计还是我犯了错误,我是否以错误的方式使用这些类?如果是,那么正确的方法是什么?
答案 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教程。