【问题标题】:JProgressbar not showing (without threads)JProgressbar 未显示(无线程)
【发布时间】:2010-07-06 14:19:24
【问题描述】:

我有一个程序需要一些时间来创建 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% 然后它关闭(就像它应该做的那样)

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

【问题讨论】:

    标签: java user-interface jprogressbar


    【解决方案1】:

    您确实需要使用线程来执行此操作。学起来比逃避好,因为它是一种常见的 GUI 框架设计。

    可以这么简单:

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

    或将invokeLater() 替换为invokeAndWait()

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 2022-08-18
        相关资源
        最近更新 更多