2 jianghao3355 jianghao3355 于 2017.09.18 17:57 提问

Java图形界面进度条显示问题 3C

这是我最近做的关于java图形界面组件的小练习,主要功能是实现文件复制并显示进度。代码没有报错,运行也无异常,但是在程序运行过程中,进度条没有随着复制进度的增长而增长,一直显示为0%,只有复制完毕才会显示100%。但是我能用getValue()方法获取当前的进度值,而这个值是没有问题的,但是他没有在进度条中显示出来,不知道什么原因。

package com.GUI;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;

public class TestControl1 {

public static float fileSize = 0;
public static float targetFileSize = 0;

public static void main(String[] args) {
    // TODO Auto-generated method stub
    JFrame jf = new JFrame("带进度条的文件复制");

    jf.setSize(500, 150);

    jf.setLocationRelativeTo(null);

    jf.setLayout(null);

    JPanel copyPanel = new JPanel();
    copyPanel.setBounds(6, 10, 450, 40);
    copyPanel.setLayout(new FlowLayout());

    JLabel srcFileAdress = new JLabel("源文件地址:");
    JTextField adressText = new JTextField("f:/jdk");
    adressText.setPreferredSize(new Dimension(100, 25));

    JLabel copyLabel = new JLabel("复制到:");
    JTextField copyText = new JTextField("f:/jdk2");
    copyText.setPreferredSize(new Dimension(100, 25));

    copyPanel.add(srcFileAdress);
    copyPanel.add(adressText);
    copyPanel.add(copyLabel);
    copyPanel.add(copyText);

    JPanel copyStartPanel = new JPanel();
    copyStartPanel.setBounds(10, 60, 450, 50);
    copyStartPanel.setLayout(new FlowLayout());

    JButton startCopyButton = new JButton("开始复制");
    startCopyButton.setPreferredSize(new Dimension(100, 30));

    JLabel fileCopyProgress = new JLabel("文件复制进度");
    JProgressBar fileProgress = new JProgressBar();
    fileProgress.setPreferredSize(new Dimension(126, 31));
    fileProgress.setMaximum(100);
    fileProgress.setStringPainted(true);

    copyStartPanel.add(startCopyButton);
    copyStartPanel.add(fileCopyProgress);
    copyStartPanel.add(fileProgress);

    jf.add(copyPanel);
    jf.add(copyStartPanel);

    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setVisible(true);

    File srcFile = new File(adressText.getText());
    calclateAllFilesize(srcFile);
    File targetFile = new File(copyText.getText());

    startCopyButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            copyTo(srcFile, targetFile,fileProgress);
        }
    });

}

public static void copyTo(File file1, File file2,JProgressBar showProgress) {

    File srcFile = file1;
    File targetFile = file2;
    JProgressBar fileProgress = showProgress;

    if (!srcFile.exists()) {
        return;
    }

    File[] allFile = srcFile.listFiles();

    for (File each : allFile) {
        File nextFile = new File(targetFile.getAbsolutePath(), each.getName());
        if (each.isDirectory()) {
            nextFile.mkdirs();
            copyTo(each, nextFile,fileProgress);
        } else {

            try(
                    FileReader fr = new FileReader(each);
                    BufferedReader br = new BufferedReader(fr);
                    FileWriter fw = new FileWriter(nextFile);
                    PrintWriter pw = new PrintWriter(fw);
                    ){
                while(true){
                    String line = br.readLine();
                    if(line == null ){
                        break;
                    }
                    pw.println(line);
                    pw.flush();
                }                   
            }catch(IOException e){
                e.printStackTrace();
            }

            targetFileSize += (float)each.length();
            int current = (int)(targetFileSize/fileSize*100);
            fileProgress.setValue(current);
            System.out.println(fileProgress.getValue());
            if(current == 100){
                JOptionPane.showMessageDialog(showProgress, "复制完毕");
            }

        }
    }

}

 public static void calclateAllFilesize(File file) {

        if (file.isFile()) {
            fileSize += (float)file.length();
        }
        if (file.isDirectory()) {
            File[] fs = file.listFiles();
            for (File f : fs) {
                calclateAllFilesize(f);
            }
        }

    }

}

1个回答

caozhy
caozhy   Ds   Rxr 2017.09.19 05:28

int current = (int)(targetFileSize/fileSize*100);
->
int current = (int)(targetFileSize*100/fileSize);

Csdn user default icon
上传中...
上传图片
插入图片