我创建了一个从其他Java类文件调用的Swing GUI。
GUI的构造函数:
public AdviceGUI(AdviceModel model) throws InvocationTargetException, InterruptedException {
System.out.println("I am called");
//AdviceGUI.model= model;
initComponents();
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(AdviceGUI.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(AdviceGUI.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(AdviceGUI.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(AdviceGUI.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
}
//check
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
try {
new AdviceGUI(model).setVisible(true);
} catch (InvocationTargetException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
函数在主Java类中调用GUI的构造函数:
public Map<String,ArrayList<String>> queryExpertGUI(AdviceModel model) throws InvocationTargetException, InterruptedException {
for(String str : model.orderedQueries) {
Utils.println("Clause:" + str);
}
//Modes file
//String mode_dir = cmdArgs.getTrainDirVal()
System.out.println("sssssssssss"+" "+model.convertModelForBK());
model.setUserClauses(model.convertModelForBK());
System.out.println("ssss"+" "+model.userClauses);
AdviceGUI a = new AdviceGUI(model);
每次调用GUI的构造函数时,用例都会调用GUI for for循环,并等到GUI(Jframe)在GUI中单击按钮后关闭。为了实现这一点,我在构造函数中使用了invokeAndWait()。但是我收到以下错误:
Caused by: java.lang.Error: Cannot call invokeAndWait from the event dispatcher thread
at java.awt.EventQueue.invokeAndWait(EventQueue.java:1303)
at java.awt.EventQueue.invokeAndWait(EventQueue.java:1296)
at edu.wisc.cs.will.Boosting.UI.AdviceGUI.<init>(AdviceGUI.java:87)
at edu.wisc.cs.will.Boosting.UI.AdviceGUI$1.run(AdviceGUI.java:90)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:301)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Java新手,任何建议都会有所帮助。
答案 0 :(得分:1)
看起来你在这里发生了无意的递归:
AdviceGUI
AdviceGUI
构造函数将尝试构建另一个AdviceGUI
实例,这将创建另一个实例,这将创建另一个...
您应该重新审核谁在创建第一个SwingUtilities.invokeLater( new Runnable() { ... } )
实例,在那时,使用invokeLater
构建并显示用户界面,然后删除{{1}来自AdviceGUI
构造函数。