swing 进度条

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.Timer;

public class test {
	JFrame frame =new JFrame("测试进度条");
	JProgressBar bar = new JProgressBar(JProgressBar.HORIZONTAL );  
	private int i =0;
public test(int length1) {
		i=length1;
	}

/*	JCheckBox indeterminate = new JCheckBox("不确定进度");     
	JCheckBox noBorder = new JCheckBox("不绘制边框");  
*/	public void init()      
	{ 
		
		Box box = new Box(BoxLayout.LINE_AXIS);          
		/*box.add(indeterminate);          
		box.add(noBorder);    */
		frame.setLayout(new FlowLayout());          
		frame.add(box);          //把进度条添加到JFrame窗口中     
		frame.add(bar);  
		//设置在进度条中绘制完成百分比       
		bar.setStringPainted(true);   
		/*noBorder.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				bar.setBorderPainted(!noBorder.isSelected());
			}
		});*/
		final SimulatedTarget target = new SimulatedTarget(i*5,frame);      
		 //以启动一条线程的方式来执行一个耗时的任务     
		 new Thread(target).start();     
		 //设置进度条的最大值和最小值,     
		 bar.setMinimum(0);       
		 //以总任务量作为进度条的最大值       
		 bar.setMaximum(target.getAmount()); 
		 bar.setPreferredSize(new Dimension(600,40));
		 Timer timer = new Timer(300 , new ActionListener()    
		 {             
			 public void actionPerformed(ActionEvent e)        
			 {                
				 //以任务的当前完成量设置进度条的value         
				 bar.setValue(target.getCurrent());     
				 }         
			 }); 
		 
			 timer.start();   
			/* indeterminate.addActionListener(   new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					
					//设置该进度条的进度是否确定         
					bar.setIndeterminate(indeterminate.isSelected());              
					bar.setStringPainted(!indeterminate.isSelected());  
				}
			 });      */
			      
			 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
			 frame.pack(); 
			 frame.setVisible(true); 
			 frame.addWindowListener(new WindowAdapter() {
	             public void windowClosing(WindowEvent e) {
	                 System.exit(0);
	             }
	         });
	     }
	     
	public static void main(String[] args)     
	{         
		String zi="";
   	 for (int i = 0; i <10; i++) {
			zi+="1234567.jpg;";
		}
		new test(zi.length()).init();
	            
	}  
	
}


//模拟一个耗时的任务
class SimulatedTarget implements Runnable  {   
	//任务的当前完成量     
	private volatile int current;  
	private JFrame frame;
	//总任务量  
	private int amount;     
	public SimulatedTarget(int amount, JFrame frame)     
	{            
		current = 0;         
		this.amount = amount;
		this.frame=frame;
	}       
	public int getAmount() 
	{         
		return amount;  
	}      
	public int getCurrent()     
	{           
		return current;   
	}     
	//run方法代表不断完成任务的过程     
	public void run()      
	{            
		while (current < amount)         
		{              
			try {        
		      Thread.sleep(50);         
		     }catch(InterruptedException e){               }       
	       current++;          
	     } 
		frame.dispose();
	}
}


©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页
实付 9.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值