java 进度条JProgressBar getValue 与显示的不同

@java 进度条JProgressBar getValue 与显示的不同

已经使用专用线程刷新setValue setStringPainted(true);

在这里插入代码片
```//按照上下两层分两个面板,上面两个标签,文本框,下面按钮,标签,进度条
//文本框提供两个文件夹地址,复制文件夹,目的文件夹(String)
public class TestGUI {
	//复制文件
	public static void copyFile(String srcFile, String destFile){
		File f = new File(srcFile);
		File f1 = new File(destFile);
		try (
				BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
				BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f1));
				) {
			byte[] by = new byte[(int) f.length()];
			//文件复制到字节数组
			bis.read(by);
			//文件不存在,创建文件父目录
			if(!f1.exists()) {
				// 创建一个空文件,创建父目录
				f1.getParentFile().mkdirs();
			}
			bos.write(by);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	}
	//复制文件夹
	public static void copyFolder(String srcFile, String destFile) {
		File f = new File(srcFile);
		File f1 = new File(destFile);
		// 以文件数组的形式,返回当前文件夹下的所有文件(不包含子文件及子文件夹)
        File[] fs= f.listFiles();
        //遍历srcFile文件夹下文件
        Boolean bo = f1.mkdir();
        //System.out.println(bo + "1");
        for (int i = 0; i < fs.length; i++) {
        	StringBuffer destFile1 = new StringBuffer(destFile);
        	//更新目标文件
    		destFile1.append("/");
    		destFile1.append(fs[i].getName());
        	//是否是文件夹
        	if(fs[i].isDirectory()) {
        		//复制子文件夹
        		copyFolder(fs[i].getAbsolutePath(), new String(destFile1));
        	}else {
        		//不创建文件,写入时创建并建立父目录
        		copyFile(fs[i].getAbsolutePath(), new String(destFile1));
        	}
		} 
	}   
	public static long getFileSize(String File) {
		File f = new File(File);
		File[] fs= f.listFiles();
		long size = 0;
		for (int i = 0; i < fs.length; i++) {
			if(fs[i].isDirectory()) {
        		
				size += getFileSize(fs[i].getAbsolutePath());
        	}else {
        		size += fs[i].length();
        	}
		}
		return size;
		
	}

	/**
	 * @Description:
	 * @param args
	 */
	public static void main(String[] args) {
		
		JFrame jf = new JFrame();
		jf.setTitle("计算器");
		jf.setSize(500, 400);
        jf.setLocation(500, 250);
        jf.setLayout(null);
        
        JLabel jl00 = new JLabel();
        jl00.setText("源文件地址:");
        jl00.setPreferredSize(new Dimension(80, 30));
        JLabel jl01 = new JLabel();
        jl01.setText("复制到:");
        jl01.setPreferredSize(new Dimension(60, 30));
        JLabel jl10 = new JLabel();
        jl10.setText("文件复制进度:");
        jl10.setPreferredSize(new Dimension(100, 30));
        JTextField jtf00 = new JTextField();
        jtf00.setPreferredSize(new Dimension(150, 30));
        JTextField jtf01 = new JTextField();
        jtf01.setPreferredSize(new Dimension(150, 30));
        JButton jb = new JButton();
        jb.setText("开始复制");
        jb.setPreferredSize(new Dimension(150, 30));
        
        JProgressBar jpb = new JProgressBar();
        //进度条最大100
        jpb.setMaximum(100);
      
      
        JPanel jp0 = new JPanel();
        jp0.setLocation(0, 20);
        jp0.setSize(500, 50);
        jp0.setLayout(new FlowLayout());
        JPanel jp1 = new JPanel();
        jp1.setLocation(0, 70);
        jp1.setSize(500, 50);
        jp1.setLayout(new FlowLayout());
        jp0.add(jl00);
        jp0.add(jtf00);
        jp0.add(jl01);
        jp0.add(jtf01);
        jp1.add(jb);
        jp1.add(jl10);
        jp1.add(jpb);

    	long[] lon = {0,0}; 
        //
        new Thread() {
        	public void run() {
        		
        		synchronized (lon) {
        			while (jtf00.getText().length() == 0) {
    					try {
    						lon.wait(100);
    					} catch (InterruptedException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    				}
					lon[0] = TestGUI.getFileSize(jtf00.getText());
					System.out.println(lon[0]);
					while (jtf01.getText().length() == 0) {
    					try {
    						lon.wait(100);
    					} catch (InterruptedException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    				}
					lon.notify();
				}
        	}
        }.start();
        //复制文件
      //更新进度条,复制大文件夹,计算文件夹大小出现0,需要使用另外的线程保证不出现0
        new Thread() {
        	
        	public void run() {
        		//当前进度是
                jpb.setValue(0);
                //显示当前进度
                jpb.setStringPainted(true);
        		//出现源文件夹大小为0,重新计算
                synchronized (lon) {
            		while (lon[0] == 0) {

            			try {
            				lon.notify();
    						lon.wait();
    					} catch (InterruptedException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    				}
				}
        		while (jpb.getValue() < 100) {

					//目标文件空间大小*100/源文件空间大小
        			
        			lon[1] = TestGUI.getFileSize(jtf01.getText())*100;
        			
        			System.out.println(lon[1] + "   " + lon[0]);
        			int in = (int) (lon[1]/lon[0]);
        			System.out.println(in);
					try {
						//睡眠时间500
						this.sleep(300);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					jpb.setValue(in);
					jpb.setStringPainted(true);
					System.out.println(jpb.getValue());
				}
        	}
        }.start();
        
        jb.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				TestGUI.copyFolder(jtf00.getText(), jtf01.getText());
			}
		});
        
        jf.add(jp0);
        jf.add(jp1);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 让窗体变得可见
        jf.setVisible(true);
        
        
	}

}

刷新进度条线程

在这里插入代码片
 new Thread() {
        	
        	public void run() {
        		//当前进度是
                jpb.setValue(0);
                //显示当前进度
                jpb.setStringPainted(true);
        		//出现源文件夹大小为0,重新计算
                synchronized (lon) {
            		while (lon[0] == 0) {

            			try {
            				lon.notify();
    						lon.wait();
    					} catch (InterruptedException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    				}
				}
        		while (jpb.getValue() < 100) {

					//目标文件空间大小*100/源文件空间大小
        			
        			lon[1] = TestGUI.getFileSize(jtf01.getText())*100;
        			
        			System.out.println(lon[1] + "   " + lon[0]);
        			int in = (int) (lon[1]/lon[0]);
        			System.out.println(in);
					try {
						//睡眠时间500
						this.sleep(300);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					jpb.setValue(in);
					jpb.setStringPainted(true);
					System.out.println(jpb.getValue());
				}
        	}
        }.start();

这个代码执行刷新进度条成功

在这里插入代码片
new Thread() {
        	
        	public void run() {
        		//当前进度是
                jpb.setValue(0);
                //显示当前进度
                jpb.setStringPainted(true);
        		while (jpb.getValue() <= 100) {
					try {
						//睡眠时间100+当前进度*100
						this.sleep(100+jpb.getValue()*100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					//每次进度加一
					jpb.setValue(jpb.getValue() + 1);
					jpb.setStringPainted(true);
				}
        	}
        }.start();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值