【问题标题】:JInternalFrame won't appearJInternalFrame 不会出现
【发布时间】:2012-09-17 19:07:59
【问题描述】:

我有一个名为 GameUpdater 的 Java 类,它扩展了 JInternalFrame。 当我将类作为程序单独运行时,它曾经扩展 JFrame,但我将其更改为 JInternalFrame 以成为更大应用程序的一部分 - 现在可以通过菜单按钮访问。

当我按下这个菜单按钮时调用的函数如下:

private void update(){

    GameUpdater gu = new GameUpdater();
    desktop.add(gu); //add to JDesktopPane
    gu.setSize(400, 300);
    gu.setVisible(true);

    gu.readMatches();//this function takes ages

    gu.setMatch("Updating database...");//this is some output to the user, displays info in the internal frame
    //try and insert into database
    for(Match m : gu.getMatches()){
        db.insertMatch(m);
    }
    gu.setMatch("DONE"); //now it shows the frame, way too late
}

gu.readMatches() 方法执行时间较长,因此它会定期更新 JInternalFrame 中的内容以显示其进度。但是,在此更新功能完成之前,不会显示框架!

就像 setVisible(true) 一直等到函数结束...

当它是 JFrame 时,它​​工作得非常好。 JInternalFrame 是否有任何奇怪的属性会导致这种情况?

干杯

【问题讨论】:

    标签: java swing concurrency event-dispatch-thread jinternalframe


    【解决方案1】:

    听起来您正在Event Dispatching Thread (EDT) 内执行一个耗时的过程,这将阻止事件队列处理(除其他外)重绘请求。

    这将使您的程序看起来好像“挂起”。

    您需要将此任务卸载到后台线程。

    阅读Concurrency in Swing,尤其是Worker Threads and SwingWorker部分

    【讨论】:

      【解决方案2】:

      问题是你阻止了你的EDT 这可以通过简单地创建一个新的Thread/Runnable thar 调用gu.readMatches(); 方法来解决:

      SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
      gu.readMatches(); //gu will have to be declared `final`
      
      gu.setMatch("Updating database...");//this is some output to the user, displays info in the internal frame
      //try and insert into database
      for(Match m : gu.getMatches()){
          db.insertMatch(m);
      }
      }
      });
      

      当然,尽管您可能希望实现JProgressBar,以便用户可以跟踪阅读的距离。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-06
        • 2015-08-14
        • 1970-01-01
        • 2011-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多