2013-10-16 3 views
0

나는이 진행 막대 만들려고 한 : 나는 방법 "setValue에"와 ProgressBar의 값을 변경하는 것을 시도했다자바에서 진행률 막대를 새로 고치는 방법은 무엇입니까?

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++); 
    } 
} 

을하지만 바는 무한 사이클 "동안", 증가,하지만하지 않습니다 변화가 없어. 뭐가 문제 야?

+0

을 처리 할 때 this advice을 따라야하지만 난 변화를 볼 수 없습니다. 뭐가 문제 야? - [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

을 만드는 시간을 투자해야 InterruptedExcetion

+1

이제 모든 것이 명확 해집니다. 나는 SSCCE를 알지 못했고, 나는 그 교훈을 배웠다고 약속하고 나는 더 질서 정연해질 것입니다. 엉망이 된 것에 사과하고 도움을 주셔서 감사합니다. – user2784212

관련 문제