简体   繁体   English

java swingworker不更新GUI

[英]java swingworker not updating GUI

Basicaly the problem is that my SwingWorker is not doing what I intend it to do, I will use some simplified code examples in here which are similar to my code but without the nasty non-relevant details. 基本上,问题是我的SwingWorker没有按照我的意愿去做,在这里我将使用一些简化的代码示例,这些示例与我的代码相似,但是没有讨厌的无关紧要的细节。

I got two classes in my case: 我有两节课:

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

The idea is that the MainPanel is a placeholder class and dynamically on runtime I add other JPanel's to it (and remove the old one). 这个想法是MainPanel是一个占位符类,在运行时动态地向它添加了其他JPanel(并删除了旧的)。

The code that does work, taken from inside MainPanel class: 起作用的代码来自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();
}

The problem here is that creating the GalleryPanel is quite slow (> one second) and I would like to display some loading circle plus prevent it from blocking the GUI, so I changed it to this: 这里的问题是创建GalleryPanel的速度很慢(>一秒钟),我想显示一些加载圆圈,并阻止它阻止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();
}

However now the GalleryPanel does not show up anymore, any help would be appreciated. 但是,现在GalleryPanel不再显示,任何帮助将不胜感激。

Extra info: The GalleryPanel's instance creation takes so long because it renders what it should show at instantition, such that the paintComponent can merely paint that image. 额外信息:GalleryPanel的实例创建需要花费很长时间,因为它呈现了实例化时应显示的内容,因此paintComponent只能绘制该图像。

Regards. 问候。

Your calls to revalidate() and repaint() occur instantaneously and before the GalleryPanel has had a chance to be created or added: 您对revalidate()repaint()调用是在创建或添加GalleryPanel之前立即进行的:

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

revalidate();
repaint();

Try instead making those calls from within the done() method, after you've added the new component: 添加新组件后,请尝试从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();        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM