我想实现一个循环功能,在循环的时候调用进度条,显示循环的进度,请问如何实现?我找到一些都是通过线程的间停来控制进度条的,说白了,我找到的都是和时间有关的进度条例子,我想要的是和循环的进度有关的例子,谢谢。

解决方案 »

  1.   

    现贴出通过线程的时间来显示的进度条方法,给需要的人,我求的是循环进度的方式显示的进度条
    import javax.swing.*;
    import java.awt.*;
    import com.borland.jbcl.layout.*;
    import java.awt.event.*;
    import java.lang.reflect.InvocationTargetException;public class jindu extends JFrame{
      JPanel jPanel1 = new JPanel();
      XYLayout xYLayout1 = new XYLayout();
      JProgressBar jProgressBar1 = new JProgressBar();
      JButton jButton1 = new JButton();
      Thread myThread;
      int num=500;
      boolean shouldStop;//should thread to be stopped
      Object lock=new Object();
      JTextArea output;  public jindu() {
        try {
          jbInit();
          this.setSize(400,300);
          this.show();
        }
        catch(Exception e) {
          e.printStackTrace();
        }  }
      public static void main(String[] args) {
        jindu jindu1 = new jindu();
      }
      private void jbInit() throws Exception {
        jPanel1.setLayout(xYLayout1);
        jButton1.setText("jButton1");
        jButton1.addActionListener(new jindu_jButton1_actionAdapter(this));
        this.addWindowListener(new jindu_this_windowAdapter(this));
        this.getContentPane().add(jPanel1,  BorderLayout.CENTER);    jProgressBar1.setValue(0);
        jProgressBar1.setStringPainted(true);    jPanel1.add(jProgressBar1,   new XYConstraints(209, 222, -1, -1));
        jPanel1.add(jButton1,   new XYConstraints(209, 180, -1, -1));
      }  void jButton1_actionPerformed(ActionEvent e) {    if(myThread==null)
        {
          myThread=new TaskThread(num);//create a task thread
          shouldStop=false;
          myThread.start();//start the task thread
        }
        else
        {//end the task
          synchronized (lock) {
            shouldStop = true;
            lock.notify(); //notify thread if stopped
          }
        }  }  class TaskThread extends Thread
    {
    private int max;public TaskThread(int max)
    {
    this.max=max;
    }
    public void run()
    {
    int min=0;
    jProgressBar1.setValue(min);
    jProgressBar1.setMinimum(min);
    jProgressBar1.setMaximum(max);Runnable runner=new Runnable()
    {
    public void run()
    {
    int value=jProgressBar1.getValue();
    value++;//increase counter value
    jProgressBar1.setValue(value);//change current value of progress bar
    }
    };for(int i=min;i<max;i++)
    {
    try
    {//block until AWT events hava been processed
    SwingUtilities.invokeAndWait(runner);
    }
    catch(InvocationTargetException e)
    {
    break;
    }
    catch(InterruptedException e)
    {
    //Ignore Exception
    }synchronized(lock)
    {
    if(shouldStop)//check if Stop is pressed
    break;
    try
    {
    lock.wait(100);
    }
    catch(InterruptedException e)
    {
    //Ignore Exception
    }
    }
    }jButton1.setEnabled(true);//enable button
    myThread=null;
    }
    }  void this_windowClosing(WindowEvent e) {
        System.exit(0);
      }
    }class jindu_jButton1_actionAdapter implements java.awt.event.ActionListener {
      jindu adaptee;  jindu_jButton1_actionAdapter(jindu adaptee) {
        this.adaptee = adaptee;
      }
      public void actionPerformed(ActionEvent e) {
        adaptee.jButton1_actionPerformed(e);
      }
    }class jindu_this_windowAdapter extends java.awt.event.WindowAdapter {
      jindu adaptee;  jindu_this_windowAdapter(jindu adaptee) {
        this.adaptee = adaptee;
      }
      public void windowClosing(WindowEvent e) {
        adaptee.this_windowClosing(e);
      }
    }
      

  2.   

    伤心了,每次我发的帖子都没人关注,晕倒ING
      

  3.   

    伤心了,每次我发的帖子都没人关注,晕倒ING
      

  4.   

    ls的我没事灌水也比你没事往OBJECT数组里面插入RS强呀 哈哈 ~~~~~~~~~~~~~~