2013-10-16 155 views
0

我试图创建这个进度条:如何在java中刷新进度条?

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”的进度的价值,但在酒吧不增加“而”循环至无穷远,但我看不到变化。怎么了?

+0

的时候,但我看不出有什么变化。怎么了? - 在[SSCCE](http://sscce.org/)表格​​中发布可编译的代码 – mKorbel

+0

你在进度条上看到什么?空的?或者发生一些动画? – UDPLover

+0

窗口打开,但栏不出现 – user2784212

回答

2

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

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

而且投入的时间,你应该遵循this advice处理InterruptedExcetion小号

+1

现在一切都更清晰。我不知道SSCCE,我保证我已经吸取了教训,我会更有条不紊。我为这个烂摊子道歉,我感谢你的帮助 – user2784212