gpt4 book ai didi

java - 在 Java Swing 中使用 invokeAndWait 时出错

转载 作者:行者123 更新时间:2023-12-02 12:49:51 24 4
gpt4 key购买 nike

我创建了一个 Swing GUI,它是从其他 Java 类文件调用的。

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 构造函数的函数时都在 for 循环中调用 GUI,并在单击 GUI 中的按钮后等待 GUI(Jframe) 关闭。为了实现这一点,我在构造函数中使用了 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,任何建议都会有帮助。

最佳答案

看起来您在这里发生了无意的递归:

public AdviceGUI(AdviceModel model) throws InvocationTargetException, InterruptedException {
// java.awt.EventQueue.invokeAndWait(new Runnable() { public void run() { try {
new AdviceGUI(model).setVisible(true);
// } catch (InvocationTargetException | InterruptedException e) { } } } });
}

AdviceGUI 构造函数将尝试构造另一个 AdviceGUI 实例,这将创建另一个实例,这将创建另一个......

您应该重新审视谁在创建第一个 AdviceGUI 实例,并且此时,使用 SwingUtilities.invokeLater( new Runnable() { ... } ) 来构造并显示 UI,并从 AdviceGUI 构造函数中删除 invokeLater

关于java - 在 Java Swing 中使用 invokeAndWait 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44637618/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com