SwingUtilities.invokeAndWait 未运行
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)
The code within the run() method is not being executed. Can anyone tell me why?
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);
}
}
}
});
Throws Error:
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您处于 EDT,则不必调用 invokeAndWait。在您的示例中,您似乎处于 EDT。
You don't have to call invokeAndWait if you are in the EDT. And in your example it seems that you are in the EDT.