2016-04-22 21 views
0

私はボタンを持っています。それをクリックすると、progressbarが始まります。一度終了したら、プログレスバーをリセットして、ボタンをもう一度クリックすると新鮮になります。さて、問題は、私が最初に実行したとき、それはOK走るが、2番目の試みで、それは実際にそれを行うにはどのように0 にプログレスバーをリセットしていないことであるJavaでJProgressBarをリセットするにはどうすればよいですか?

else if (action == detectButton) { 

      // pb.setVisible(true); 

      SwingUtilities.invokeLater(new Runnable() { 

       @Override 
       public void run() { 
        pb.setValue(0); 
        pb.repaint(); 
       } 
      }); 

      for (int i = 1; i <= 10; i++) { 
       final int val = i; 
       SwingUtilities.invokeLater(new Runnable() { 
        public void run() { 
         pb.setValue(val); 
         pb.repaint(); 
         try { 
          java.lang.Thread.sleep(100); 
         } catch (InterruptedException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
        } 

       }); 

      } 

??:
はここに私のコードです

EDIT:それので、

+0

**第二の試みを何が起こっている**? –

+0

プログレスバーが一度終了してから2回目にクリックすると、進行状況は完全にリセットされます。つまりリセットされません。 –

+0

質問にさらにコードを追加できますか?あなたは実行可能ファイルを掲示しましたが、それは自分のスレッドにプログレスバーをリセットして2秒間待っていますが、forループは10個の新しいランナブルを実行します –

答えて

0

あなたのJProgressBarのはリセットされません..私はプログレスバー毎回を塗り替えていますが、私は10%、次に20%が起こったことを示すために十分に滑らかではないことを見ました最初のRunnableが終了した後に再描画されます。しかし、あなたのjava.lang.Thread.sleep(2000);はこれを防ぎます。ここで

+0

プログレスバーがいっぱいになったらもう一度ボタンをクリックすると、完全に残ります。私もforloopでsleep()を与えてみました。 –

0

は、プログレスバーを使用する方法、完全な例です:

public class TestWindow 
{ 

    private JFrame frame; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       try 
       { 
        TestWindow window = new TestWindow(); 
        window.frame.setVisible(true); 
       } 
       catch (Exception e) 
       { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 
    public TestWindow() 
    { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() 
    { 
     frame = new JFrame(); 
     frame.setBounds(100, 100, 450, 300); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(null); 

     final JProgressBar progressBar = new JProgressBar(); 
     progressBar.setBounds(10, 11, 414, 14); 
     frame.getContentPane().add(progressBar); 

     JButton btnNewButton = new JButton("Reset"); 
     btnNewButton.setBounds(10, 228, 89, 23); 
     frame.getContentPane().add(btnNewButton); 

     JButton button = new JButton("Run"); 
     button.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent e) 
      { 
       Runnable update = new Runnable() 
       { 

        @Override 
        public void run() 
        { 
         for (int i = 1; i <= 100; i++) 
         { 
          final int val = i; 

          SwingUtilities.invokeLater(new Runnable() 
          { 
           public void run() 
           { 
            progressBar.setValue(val); 
            progressBar.repaint(); 
           } 

          }); 

          try 
          { 
           java.lang.Thread.sleep(100); 
          } 
          catch (InterruptedException e1) 
          { 
           // TODO Auto-generated catch block 
           e1.printStackTrace(); 
          } 
         } 
        } 
       }; 
       Thread t = new Thread(update); 
       t.start(); 
      } 
     }); 

     button.setBounds(109, 228, 89, 23); 
     frame.getContentPane().add(button); 

     btnNewButton.addActionListener(new ActionListener() 
     { 

      @Override 
      public void actionPerformed(ActionEvent e) 
      { 
       SwingUtilities.invokeLater(new Runnable() 
       { 

        @Override 
        public void run() 
        { 
         progressBar.setValue(0); 
         progressBar.repaint(); 
        } 
       }); 
      } 
     }); 

    } 
} 
+0

私はそれを実行し、それをリセットするための2つのボタンを使用する必要があります..私は1つのボタンを押してそれを行うことはできません?または終了時にプログレスバーを隠すだけで、ボタンを初期状態からやり直して表示することができますか? –