【问题标题】:SwingUtilities.invokeLater with infinite loop, not continuing?SwingUtilities.invokeLater 无限循环,不继续?
【发布时间】:2016-04-24 09:18:10
【问题描述】:

我有一个无限循环等待SwingUtilities.invokeLater 中带有Runnable 接口的事件,但执行没有继续......为什么?

//here hello3 prints in cmd but hello2 will not execute
System.out.println("hello3");

//showMessage();
SwingUtilities.invokeLater(
    new Runnable(){
        public void run() {
            while(true) {
                jEditorPane1.setText(s9);
            }
        }
    }
);

System.out.println("hello2");

【问题讨论】:

  • Concurrency in Swing 开头。您正在阻止 EDT。 invokeLater 在未来某个时间点在 EDT 上执行 run 方法 Runnable

标签: java swing invokelater


【解决方案1】:

SwingUtilities.invokeLater 将在事件调度线程内执行 run 方法。

如果您向我们展示的所有代码都在事件调度线程中执行(例如,如果它在 actionListener 中执行),则线程在无限循环中变得“冻结”。

【讨论】:

  • 不,它不在任何处理程序中,我使用 netbeans 表单来设计 gui,但它在一个 swingutilities.invokelater 中生成所有 gui 代码,但我创建了另一种方法,在这个类中包含这个代码,我将调用使用从此类创建的对象
  • Forum_main fm = new Forum_main(); fm.setVisible(true); fm.getdata(uid, tid); close();
  • 类调用看起来像这样
  • mainjava.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Forum_main().setVisible(true); } });
  • 在getdata()函数中System.out.println("hello3"); //showMessage(); SwingUtilities.invokeLater( new Runnable(){ public void run() { while(true) { jEditorPane1.setText(s9); } } } ); System.out.println("hello2");
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-11
  • 1970-01-01
  • 2011-08-19
  • 2021-03-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多