jprogressbar is non tegrant

发布于 2025-01-31 11:57:51 字数 821 浏览 1 评论 0原文

因此,我正在尝试制作一个下载器,该下载器显示了使用进度栏的下载进度。 但是我有问题,因为它实际上并没有更新进度栏。基本上,当它是蓝色的时候,它会保持白色。如果有人可以提供帮助,则代码在下面。

JProgressBar progressBar = new JProgressBar(0, ia);
con.add(progressBar, BorderLayout.PAGE_START);
con.validate();
con.repaint();
progressBar = new JProgressBar(0, ia);
progressBar.setValue(0);
System.out.print("Downloading Files");
while ((count = in.read(data, 0, downloadSpeed)) != -1){
    fout.write(data, 0, count);
    if (count >= 2){
        progressBar.setString("Downloading : " + ia + " @ " + count + "Kbs per second");
    } else {
        progressBar.setString("Downloading : " + ia + " @ " + count + "Kb per second");
    }
    progressBar.setValue(count);
    con.add(progressBar, BorderLayout.PAGE_START);
    try{
        Thread.sleep(1000);
    } catch (Exception e){}
}

So i'm trying to make a downloader which shows the progress of the download with a progress bar.
But i'm having problems since it doesn't actually update the progress bar. Basically it stays white, when it is meant to be blue. If anyone could help, code is below.

JProgressBar progressBar = new JProgressBar(0, ia);
con.add(progressBar, BorderLayout.PAGE_START);
con.validate();
con.repaint();
progressBar = new JProgressBar(0, ia);
progressBar.setValue(0);
System.out.print("Downloading Files");
while ((count = in.read(data, 0, downloadSpeed)) != -1){
    fout.write(data, 0, count);
    if (count >= 2){
        progressBar.setString("Downloading : " + ia + " @ " + count + "Kbs per second");
    } else {
        progressBar.setString("Downloading : " + ia + " @ " + count + "Kb per second");
    }
    progressBar.setValue(count);
    con.add(progressBar, BorderLayout.PAGE_START);
    try{
        Thread.sleep(1000);
    } catch (Exception e){}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄想挽回 2025-02-07 11:57:52

正如@happyburnout所指出的那样,您会更好地在单独的线程中使用 swingworker 可能是您正在做的事情的最佳解决方案。

主要原因是您要阻止事件派遣线程(( aka edt)运行,防止处理任何重新粉刷请求(和其他UI重要的事情)。

您应该通过

现在,这几乎直接从API文档中获取,但是用swingwoker给出一个jprogressbar

“ worker” ...

public class Worker extends SwingWorker<Object, Object> {

    @Override
    protected Object doInBackground() throws Exception {

        // The download code would go here...
        for (int index = 0; index < 1000; index++) {

            int progress = Math.round(((float)index / 1000f) * 100f);
            setProgress(progress);

            Thread.sleep(10);

        }

        // You could return the down load file if you wanted...
        return null;

    }

“ progress pane”

public class ProgressPane extends JPanel {

    private JProgressBar progressBar;

    public ProgressPane() {

        setLayout(new GridBagLayout());
        progressBar = new JProgressBar();

        add(progressBar);

    }

    public void doWork() {

        Worker worker = new Worker();
        worker.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if ("progress".equals(evt.getPropertyName())) {
                    progressBar.setValue((Integer) evt.getNewValue());
                }
            }

        });

        worker.execute();

    }

}

的黄金规则

  • 记住摇摆,从不,从不,永远不要更新ui 来自任何thread其他的组件,那么EDT
  • 总是在其他thread上执行耗时的任务
  • (某些东西,某些东西,关于布局经理的东西 - 这更多是个人事物;))

您将在摇摆方面度过快乐,轻松的时光:D

As @happyburnout has pointed out, you'd be better of processing you download in a separate thread, using a SwingWorker is probably the best solution for what you are doing.

The main reason is you're blocking the Event Dispatching Thread (AKA EDT) from running, preventing any repaint requests (and other UI important things) from been processed.

You should have a read through

Now this is taken almost directly from the API docs, but gives a basic idea of a SwingWoker with a JProgressBar

The "Worker"...

public class Worker extends SwingWorker<Object, Object> {

    @Override
    protected Object doInBackground() throws Exception {

        // The download code would go here...
        for (int index = 0; index < 1000; index++) {

            int progress = Math.round(((float)index / 1000f) * 100f);
            setProgress(progress);

            Thread.sleep(10);

        }

        // You could return the down load file if you wanted...
        return null;

    }

The "progress pane"

public class ProgressPane extends JPanel {

    private JProgressBar progressBar;

    public ProgressPane() {

        setLayout(new GridBagLayout());
        progressBar = new JProgressBar();

        add(progressBar);

    }

    public void doWork() {

        Worker worker = new Worker();
        worker.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if ("progress".equals(evt.getPropertyName())) {
                    progressBar.setValue((Integer) evt.getNewValue());
                }
            }

        });

        worker.execute();

    }

}

Remember the golden rules of Swing

  • Never, never, never update a UI component from any Thread other then the EDT
  • Always perform time consuming tasks on a different Thread
  • (Something, something, something about layout managers - that's more of a personal thing ;))

And you will have a happy and easy time with Swing :D

与Swingworker结合使用。
在此处查看一个示例:
“ nofollow”> swingworker and Progress bar

@hovercraft @hhovercraft:@hhovercraft:对您。请允许我参考相应的,我认为这最好解释情况。

Use a combination with SwingWorker.
See an example here:
SwingWorker and Progress Bar

@Hovercraft: You're right. Allow me to refer to the corresponding SwingWorker page of JavaDoc, in my opinion this explains the situation best.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文