java swingworker没有更新GUI

ski*_*iwi 1 java swing swingworker

基本上问题是我的SwingWorker没有按照我的意图去做,我会在这里使用一些简化的代码示例,它们与我的代码类似,但没有令人讨厌的不相关细节.

在我的案例中我有两个班:

  1. MainPanel扩展JPanel
  2. GalleryPanel扩展了JPanel

我们的想法是MainPanel是一个占位符类,并在运行时动态地向其添加其他JPanel(并删除旧的).

从MainPanel类内部获取的代码:

public void initGalleryPanel() {
    this.removeAll();

    double availableWidth = this.getSize().width;
    double availableHeight = this.getSize().height;

    double width = GamePanel.DIMENSION.width;
    double height = GamePanel.DIMENSION.height;

    double widthScale = availableWidth / width;
    double heightScale = availableHeight / height;
    final double scale = Math.min(widthScale, heightScale);

    add(new GalleryPanel(scale));

    revalidate();
    repaint();
}
Run Code Online (Sandbox Code Playgroud)

这里的问题是创建GalleryPanel非常慢(> 1秒)我想显示一些加载圈并防止它阻止GUI,所以我把它更改为:

public void initGalleryPanel() {
    this.removeAll();

    double availableWidth = this.getSize().width;
    double availableHeight = this.getSize().height;

    double width = GamePanel.DIMENSION.width;
    double height = GamePanel.DIMENSION.height;

    double widthScale = availableWidth / width;
    double heightScale = availableHeight / height;
    final double scale = Math.min(widthScale, heightScale);

    new SwingWorker<GalleryPanel, Void>() {
        @Override
        public GalleryPanel doInBackground() {
            return new GalleryPanel(scale);
        }

        @Override
        public void done() {
            try {
                add(get());
            } catch (InterruptedException | ExecutionException ex) {
                Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }.execute();        

    revalidate();
    repaint();
}
Run Code Online (Sandbox Code Playgroud)

然而,现在GalleryPanel不再出现,任何帮助将不胜感激.

额外信息:GalleryPanel的实例创建需要很长时间,因为它会呈现它应该在即时显示的内容,这样paintComponent只能绘制该图像.

问候.

Hov*_*els 5

您可以在GalleryPanel有机会创建或添加之前立即拨打电话revalidate()repaint()立即发生:

    @Override
    public void done() {
        try {
            add(get());
        } catch (InterruptedException | ExecutionException ex) {
            Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}.execute();        

revalidate();
repaint();
Run Code Online (Sandbox Code Playgroud)

done()在添加新组件后,尝试从方法中进行调用:

    @Override
    public void done() {
        try {
            add(get());
            revalidate();
            repaint();
        } catch (InterruptedException | ExecutionException ex) {
            Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}.execute();        
Run Code Online (Sandbox Code Playgroud)