run()方法中的代码未被执行。谁能告诉我为什么?
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if(creator == null) {
String[] args = getArguments();
try {
writeSettingsFile(args);
} catch(IOException io) {}
consolePanel.addLine("Starting...");
bar.setIndeterminate(true);
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
MessageConsole console = new MessageConsole(getConsole().getTextPane(), self, null);
console.redirectOut(new Color(240, 240, 240), null);
console.redirectErr(Color.RED, null);
console.setMessageLines(consolePanel.getHeight() / 17);
try {
SomeApp.main(getArguments());
} catch (Exception ex) {
Logger.getLogger(OSXWorldPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
} catch (InterruptedException ex) {
Logger.getLogger(OSXWorldPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(OSXWorldPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
引发错误:
Exception in thread "AWT-EventQueue-0" java.lang.Error: Cannot call invokeAndWait from the event dispatcher thread
at java.awt.EventQueue.invokeAndWait(EventQueue.java:1017)
at javax.swing.SwingUtilities.invokeAndWait(SwingUtilities.java:1320)
答案 0 :(得分:3)
如果你在EDT,你不必调用invokeAndWait。在你的例子中,似乎你在EDT。