1

我有一个程序需要一些时间来创建 pdf 文件我想向用户显示进度

当我完成制作一个 pdf 文件时,我尝试调用进度条来更新其状态:

ProgressDialog progress = new ProgressDialog(instance, numberOfInvoices);
progress.setVisible(true);
progress.setAlwaysOnTop(true);

for(int i = 0 ; i<numOfPdfs ; i++){
    progress.change(i + 1);
}

进度对话框如下所示:

public class ProgressDialog extends JDialog {

    private JProgressBar progressBar;

    public ProgressDialog(Window parent, int aantal) {
        super(parent);

        this.setPreferredSize(new Dimension(300, 150));

        progressBar = new JProgressBar(0, aantal);
        progressBar.setValue(0);
        progressBar.setSize(new Dimension(280, 130));
        this.add(progressBar, BorderLayout.CENTER);

        this.pack();
        this.setLocationRelativeTo(parent);
    }

    public void change(int aantal) {
        if (aantal < progressBar.getMaximum() && aantal >= 0) {
            progressBar.setValue(aantal);
        }
    }
}

我得到的只是一个空窗(白色)

我在这个论坛上环顾四周,但踏板解决方案似乎太复杂了

在计算 2 个 pdf 文件之间我应该能够更新 gui,对吧?

好的科林

我试过这个:(完整的课程)

public class ProgressDialog extends JDialog {

    private JProgressBar progressBar;
    private String message;
    private static int number;

    public ProgressDialog(Window parent, int max, String message) {
    super(parent);

    this.message = message;
    this.setPreferredSize(new Dimension(300, 150));

    progressBar = new JProgressBar(0, max);
    progressBar.setValue(0);
    progressBar.setSize(new Dimension(280, 130));
    progressBar.setString(message + " 0/" + progressBar.getMaximum());
    progressBar.setStringPainted(true);
    this.add(progressBar, BorderLayout.CENTER);

    this.pack();
    this.setLocationRelativeTo(parent);

    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));


    }

    public void change(int number) {
    ProgressDialog.number = number;
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
        int number = ProgressDialog.getAantal();
        if (number < progressBar.getMaximum() && number >= 0) {
            progressBar.getModel().setValue(number);
            progressBar.setString(message + " " + progressBar.getValue() + "/" + progressBar.getMaximum());
            System.out.println("progressbar update: " + number + "/" + progressBar.getMaximum());
        } else {
            setCursor(null);
            System.out.println("progressbar terminated");
        }
        }
    });
    }

    public static int getAantal(){
    return number;
    }
}

现在它仍然显示空窗口,但是当所有 pdf 都准备好时,它确实显示进度条完成 0% 然后它关闭(就像它应该做的那样)

知道为什么它在过程中保持空白吗?

4

2 回答 2

5

你确实需要用线程来做。学起来比逃避好,因为它是一种常见的 GUI 框架设计。

它可以很简单:

public void change(int aantal) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            if (aantal < progressBar.getMaximum() && aantal >= 0) {
                progressBar.setValue(aantal);
            }
        }
    });
}

或替换invokeLater()invokeAndWait().

于 2010-07-06T14:24:19.943 回答
0

阅读 Swing 教程中关于并发的部分,以了解为什么需要线程来解决这个问题。另请阅读“如何使用进度条”部分以获取工作示例。

于 2010-07-06T15:18:15.490 回答