谨慎使用 java 1.7。有一个错误。rancidfishbreath发布的解决方案在java 1.6中是完美的,但导致Swing应用程序永远不会退出java 1.7。在JDK 1.7下,您必须在事件调度线程中安装新的EvenQueue...在JDK 1.6中...写一次,到处跑;-)
这是一个通用的解决方案...希望,1.8不会改变它;-)
import java.awt.AWTEvent;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.lang.reflect.InvocationTargetException;
public class QueueTest {
public static void main(String[] args) throws InterruptedException, InvocationTargetException {
if (!isJava7Like()) setQueue();
EventQueue.invokeAndWait(new Runnable() {
public void run() {
if (QueueTest.isJava7Like()) setQueue();
System.out.println("Run");
}
});
}
private static void setQueue() {
EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
eventQueue.push(new MyEventQueue());
}
private static boolean isJava7Like() {
return Float.parseFloat(System.getProperty("java.specification.version")) > 1.6;
}
private static class MyEventQueue extends EventQueue {
public void postEvent(AWTEvent theEvent) {
System.out.println("Event Posted");
super.postEvent(theEvent);
}
}
}