我想要实现的是
- 在某种状态下,一个类应该自动隐藏其他类显示的对话框
- 当程序状态发生变化时,应显示隐藏的对话框
问题:
- 这不适用于 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
我的问题是,我做错了什么?这是每个设计还是我犯了错误,我是否以错误的方式使用这些类?如果是,正确的方法是什么?