本文介绍了在多任务应用程序中管理GUI和EDT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个Java应用程序,用于创建和提取存档-如 WinRAR .您可以使用多线程同时创建多个档案.最近,我想在归档创建期间以每次创建的新JFrame中的JProgressBar的形式添加信息状态.

I developed a Java application for creating and extracting an archive - like WinRAR. You can create several archives at the same time with multithreading. And recently, I wanted to add an information status during the archive creation in the form of JProgressBar in a new JFrame at every creation.

但是我的问题是在新的状态框架和创建档案的线程中生成信息.这就是为什么我在存档线程中创建JFrame以便当前更新进度条.

But my problem is generating information in the new status frame and the thread which create the archive. That's why I create the JFrame in the archive thread for updating the progress bar currently.

但是,就像我可以在各种各样的信息源中以及您的答案/评论中阅读它一样,它与Java 摇摆和性能;我无法在 EDT 的其他位置创建swing对象.

But like I could read it in a diverse information source and on your answers/comments, it's against Java Swing and performance; I can't create swing object elsewhere that the EDT.

但是,我该如何解决我的问题呢?如何建立归档文件及其状态JFrame(通过JProgressBar)之间的通信?

But then, how should I solve my problem? How can I etablish communication between the writing of my archive and its status JFrame (with JProgressBar)?

我实现了SwingWorker来管理我的应用程序中的GUI.现在完成了,我还有另一个问题:

I implemented SwingWorker to manage the GUI in my application. Now it's done, I have an other question:

使用SwingWorker,如何通过状态框架按钮上的事件来执行后台任务? (例如:暂停压缩或停止压缩.)

With SwingWorker, how do I act on the background task with an event on status Frame's button? (Example: pause compression or stop it.)

推荐答案

其他人建议,最好的方法是使用SwingWorker.

As suggested by others, the best way is to use SwingWorker.

SwingWorker属性是可侦听的,并且始终在EDT中调用侦听器,因此,您可以执行以下操作:

SwingWorker properties are listenable and listeners are always called in the EDT, thus, you could do something like:

public class ArchivingWorker extends SwingWorker<Void, Void> {
    JProgressBar progressBar = null;
    // Other members here...
    ...

    public ArchivingWorker(...) {
        // Any specific initialization here (in EDT)
        addPropertyChangeListener(new PropertyChangeListener() {
            @Override void propertyChange(PropertyChangeEvent e) {
                if (    "state".equals(e.getPropertyName())
                    &&  e.getNewValue() == StateValue.STARTED) {
                    // Background thread has just started, show a progress dialog here
                    progressBar = new JProgressBar();
                    ...
                }
                else if ("progress".equals(e.getPropertyName())) {
                    // Update progress bar here with e.getNewValue()
                    ...
                }
            }
        });
    }

    @Override protected Void doInBackground() {
        // Archiving process here and update progress from time to time
        setProgress(progress);

        return null;
    }

    @Override protected void done() {
        // Ensure that archiving process worked correctly (no exception)
        try {
            get();
        } catch (Exception e) {
            // Handle exception (user feedback or whatever)
        } finally {
            // Close progress dialog
            ...
        }
    }
}

然后您可以根据需要使用ArchivingWorker:

Then you can use ArchivingWorker as you need it:

ArchivngWorker worker = new ArchivingWorker(...);
worker.execute();

这篇关于在多任务应用程序中管理GUI和EDT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 20:55