首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Java Swing中使用invokeAndWait时出现错误

在Java Swing中使用invokeAndWait时出现错误
EN

Stack Overflow用户
提问于 2017-06-20 02:35:05
回答 1查看 462关注 0票数 0

我已经创建了一个从其他Java类文件调用的Swing GUI。

GUI的构造函数:

代码语言:javascript
复制
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的构造函数的函数:

代码语言:javascript
复制
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()。但我得到以下错误:

代码语言:javascript
复制
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新手来说,任何建议都会很有帮助。

EN

回答 1

Stack Overflow用户

发布于 2017-06-20 03:04:36

看起来你在这里进行了无意的递归:

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44637618

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档