利用线程来处理java中进度条动态改…

来源:互联网 发布:萧技网络电视台 编辑:程序博客网 时间:2024/06/02 04:06

//继承JProgressBar类

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

importjavax.swing.border.*;

public class OAGProgressBarextends JProgressBar{
  
    int value=0;
   private  int count = 0;
    privatestatic long total = 100;
    booleanischanged=true;
    staticboolean isCompleted=true;
   
    publicOAGProgressBar(){
       this.setForeground(SystemColor.inactiveCaption);
       this.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
       this.setOrientation(JProgressBar.HORIZONTAL);
       this.setBorderPainted(true);
       this.setMinimum(0);
       this.setMaximum(100);
      
       //定义两个线程完成相应的工作
       
    }
   
   
   //--------------------------------------------------
    
    public void setAtribute(){
    
    }
   //--------------------------------------------------
    public voidrunWork(){
       
       if(!isCompleted) {
           JOptionPane.showMessageDialog(null,"正在工作请稍候!","提示",JOptionPane.INFORMATION_MESSAGE);
           return;
       }
       
       ischanged=true;
       count=0;
       
       isCompleted=false;
       
       
       
       Runnable runA = new Runnable(){
           public void run(){
               try{
                   //oth.setProgressBarValue();
                   setProgressBarValue();
               }catch(InterruptedException e){
                   
               }
           }
       }; //end runA
       
       
       Runnable runB = new Runnable(){
           public void run(){
               try{
                  // oth.insertOAGData(total);
                   insertOAGData(100);
               }catch(InterruptedException e){
                   
               }
           }
       }; //end runA
       


       Thread ta = new Thread(runA,"ThreadA");
       Thread tb = new Thread(runB,"ThreadB");
      
       tb.start();

       ta.start();


    }
   
    publicsynchronized void setProgressBarValue()
    throwsInterruptedException
    {
      //count <total&&
       while(  !isCompleted){
           while(!ischanged){
               System.out.println("setProgressBarValue--wait  " + count);
               wait();
           }
           System.out.println("setProgressBarValue----getValue: "+count+"--runing   "+ isCompleted);
           setValue(count);
           ischanged = false;
           notifyAll();
    }
       System.out.println("setProgressBarValue--Completed! "+ isCompleted);
       if(count>=total){
           
           ischanged=false;
           isCompleted = true;
           System.out.println("setProgressBarValue--Completed!");
           notifyAll();
       }
    }
    publicsynchronized void insertOAGData(long n)
    throwsInterruptedException
    {
       long i=0;
   
         while(i<n &&!isCompleted) 
         {
               while(ischanged){
                   System.out.println("insertOAGData--waiting   "+i);
                   wait();
               }
               i++;
               System.out.println("insertOAGData--running +  "+i);
               Thread.sleep(50);
               count++;
               ischanged = true;
               notifyAll();
          }
         System.out.println("insertOAGData--Completed!");
         if(i>=n){
             isCompleted = true;
             ischanged = true;
             System.out.println("insertOAGData--Completed!");
            notifyAll();
         }
    }

}

//主类调用OAGProgressBar动态显示

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;

 classProgressBarDemo implements ActionListener,ChangeListener
{
    JFrame f =null;
   OAGProgressBar progressbar;
    JLabellabel;
    Timertimer;
    JButtonb;

    publicProgressBarDemo()
    {
       f = new JFrame("progressbar Example");
       Container contentPane = f.getContentPane();
       label = new JLabel(" ",JLabel.CENTER);
       progressbar = new OAGProgressBar();
       progressbar.setValue(0);
       progressbar.addChangeListener(this);
       progressbar.setPreferredSize(new Dimension(800,20));
       JPanel panel = new JPanel();
       b = new JButton("Start");
       b.setForeground(Color.orange);
       b.addActionListener(this);
       panel.add(b);

       timer = new Timer(50,this);

       contentPane.add(panel,BorderLayout.NORTH);
       contentPane.add(progressbar,BorderLayout.CENTER);
       contentPane.add(label,BorderLayout.SOUTH);

       f.pack();
       f.setVisible(true);

       f.addWindowListener(new WindowAdapter() {
           public void windowClosing(WindowEvent e) {
               System.exit(0);
           }
       });
    }

    publicstatic void main(String[] args)
    {
       new ProgressBarDemo();
    }

    publicvoid actionPerformed(ActionEvent e)
    {
       progressbar.setValue(0);
       progressbar.runWork();
 
    }

    publicvoid stateChanged(ChangeEvent e1)
    {
       int value = progressbar.getValue();

       if(e1.getSource() == progressbar)
       {
           label.setText("目前已完成进度:"+Integer.toString(value)+" %");
           label.setForeground(Color.blue);
       }
    }
}
原创粉丝点击