【问题标题】:How to refresh a progress bar in java?如何在java中刷新进度条?
【发布时间】:2013-10-17 01:02:23
【问题描述】:

我已尝试创建此进度条:

public class ProgressBar extends JFrame{

private JButton fine = new JButton("Chiudi");
final JProgressBar jp;
JPanel body;
JLabel banner1;
JLabel banner2;
JPanel testo;
JTextArea areatesto;
JPanel provapannello;
JTextArea provatesto;
JPanel pannellofine;

public ProgressBar() {
     super("loading");

    setSize(440, 400);
    setResizable(true);
    Container content = getContentPane();
    content.setBackground(Color.YELLOW);
    body = new JPanel();
    body.setLayout(new FlowLayout());
    ImageIcon image1 = new ImageIcon("prova1.png");
    banner1 = new JLabel();
    banner2 = new JLabel();
    banner1.setIcon(image1);
    JTextArea prova = new JTextArea("bar", 2, 40);
    prova.setBackground(Color.LIGHT_GRAY);
    prova.setLineWrap(true);
    body.add(banner1);
    body.add(prova);
    body.add(banner2);
    banner.setBounds(30, 80, 120, 120);
    testo = new JPanel();
    areatesto = new JTextArea("Attendi Per Favore, Sto Preparando Il Tuo PDF", 2, 33);
    areatesto.setLineWrap(true);
    testo.add(areatesto);
   ImagePanel progress_background = new ImagePanel("p.jpg"); 

    UIManager.put("ProgressBar.background", new Color(29, 29, 29));
    UIManager.put("ProgressBar.foreground", new Color(16, 95, 173));
    UIManager.put("ProgressBar.selectionBackground", new Color(214, 214, 214));
    UIManager.put("ProgressBar.selectionForeground", new Color(29, 29, 29));

    jp = new JProgressBar();
    jp.setUI(new BasicProgressBarUI());

    jp.setBounds(0, 205, 434, 25);
    jp.setMinimum(0);
    jp.setMaximum(100);
    jp.setStringPainted(true);
    jp.setBorder(null);
    progress_background.add(jp);
    provapannello = new JPanel();
    provatesto = new JTextArea("prova", 2, 70);


    provatesto.setBackground(Color.LIGHT_GRAY);
    provapannello.setBounds(0, 226, 500, 100);
    provapannello.add(provatesto);

    content.add(provapannello);
    content.add(progress_background);

    pannellofine = new JPanel();

    pannellofine.add(fine);
    pannellofine.setBounds(340, 330, 100, 100);
    pannellofine.setVisible(false);

    content.add(pannellofine);
    content.add(testo);

    content.add(body);
    Thread runner;
    jp.setValue(0);
    setVisible(true);
    public void setValue(final int j) {

    Runnable target = new Runnable() {
        @Override
        public void run() {
            jp.setValue(j);
        }
    };

    SwingUtilities.invokeLater(target);

}
    public static void main(String Args[]) {
    ProgressBar p = new ProgressBar();


    int i = 0;
    while(true){
        System.out.println("work");
        try {
            Thread.sleep(500);
        } catch (InterruptedException ex) { }

        p.setValue(i++);
    }
}

我尝试使用“setValue”方法更改 ProgressBar 的值,但进度条没有增加,“while”循环到无穷大,但我看不到任何变化。怎么了?

【问题讨论】:

  • 但我没有看到任何变化。怎么了? - 在SSCCE 表单中发布可编译代码
  • 你在进度条上看到了什么?空的?还是发生了一些动画?
  • 窗口打开但栏没有出现
  • 查看 SwingWorker 类:docs.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html。它甚至使用 ProgressBar 的更新作为文档示例。
  • @user2784212 如果您需要帮助,您应该提供重现错误的示例代码,而不是一些甚至无法编译的代码。在我的机器上,这段代码在清理完混乱后没有问题。所以也许你唯一需要做的就是清理你的原始源代码。

标签: java multithreading swing


【解决方案1】:

我已经删除了大部分不必要的代码(并修复了您的编译错误),并且您的示例有效(我已经更改了延迟和增量,因为再次出于复制目的,您不应该浪费人们的时间)

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

public class ProgressBar extends JFrame {

    private JProgressBar progressBar;

    public ProgressBar() {
        super("loading");
        setSize(200, 100);
        Container content = getContentPane();
        content.setLayout(new BorderLayout());
        progressBar = new JProgressBar();
        progressBar.setMinimum(0);
        progressBar.setMaximum(100);
        progressBar.setStringPainted(true);
        progressBar.setBorder(null);
        content.add(progressBar, BorderLayout.SOUTH);
        setVisible(true);
    }

    void updateProgress(final int newValue) {
        progressBar.setValue(newValue);
    }

    public void setValue(final int j) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                updateProgress(j);
            }
        });
    }

    public static void main(final String Args[]) {
        ProgressBar myProgressBar = new ProgressBar();
        int i = 0;
        while (i <= 100) {
            System.out.println("" + i + "%");
            myProgressBar.setValue(i);
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                break;
            }
            i = i + 5;
        }
    }
}

您真的应该花时间创建SSCCE

另外,在处理InterruptedExcetions 时,您应该关注this advice

【讨论】:

  • 现在一切都清楚了。我不知道SSCCE,我保证我已经吸取了教训,我会更有条理。我为造成的混乱道歉,感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2012-05-04
  • 1970-01-01
  • 1970-01-01
  • 2011-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多