今天写一个具有进度条的文件拷贝小程序。也就是大家平常经常看见拷贝文件时候的进度条。当然此程序是用Java实现的。好了闲话少说,马上给出具体的程序代码和截图。
1、首先创建一个CopyFrame类。这类是文件拷贝的窗口主界面
package javait.org.filecopy;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class CopyFrame extends JFrame {
private ContentPanel contentPanel;
private ProgressBarPanel progressBarPanel;
public CopyFrame() {
super("文件拷贝小程序");
init();
}
private void init() {
this.setSize(350,250);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
contentPanel = new ContentPanel();
progressBarPanel = new ProgressBarPanel();
contentPanel.setProgressBarPanel(progressBarPanel);
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screen = kit.getScreenSize();
Dimension frame = this.getSize();
if (screen.width < frame.width) {
frame.width = screen.width;
}
if (screen.height < frame.height) {
frame.height = screen.height;
}
int locationWidth = (screen.width - frame.width) / 2;
int locationHeight = (screen.height - frame.height) / 2;
this.setLocation(locationWidth, locationHeight);
this.getContentPane().add(contentPanel);
this.getContentPane().add(progressBarPanel, BorderLayout.SOUTH);
this.setVisible(true);
}
public static void main(String[] args) {
new CopyFrame();
}
}
2、创建ContentPanel类,此类主要放置按钮、文本框等基本组件。
package javait.org.filecopy;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ContentPanel extends JPanel implements ActionListener, Runnable {
private JButton srcButton;
private JButton targetButton;
private JButton copyButton;
private JTextField srcTextField;
private JTextField targetTextField;
private ProgressBarPanel progressBarPanel;
private Thread thread;
private FileInputStream fis;
private FileOutputStream fos;
String fileName = ""; //文件的名字
public void setProgressBarPanel(ProgressBarPanel progressBarPanel) {
this.progressBarPanel = progressBarPanel;
}
public ContentPanel() {
srcButton = new JButton("源文件");
targetButton = new JButton("目标文件");
copyButton = new JButton("拷贝");
srcTextField = new JTextField(30);
targetTextField = new JTextField(30);
this.setLayout(null); //设置布局
this.add(srcTextField);
srcTextField.setBounds(55, 30, 140, 30);
this.add(srcButton);
srcButton.setBounds(195, 30, 80, 30);
this.add(targetTextField);
targetTextField.setBounds(55, 63, 140, 30);
this.add(targetButton);
targetButton.setBounds(195, 63, 80, 30);
this.add(copyButton);
copyButton.setBounds(195, 96, 80, 30);
copyButton.addActionListener(this);
srcButton.addActionListener(this);
targetButton.addActionListener(this);
}
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
Object o = arg0.getSource();
if (o == copyButton) {
String src = srcTextField.getText();
String target = targetTextField.getText();
if ("".equals(src)) {
JOptionPane.showMessageDialog(null, "请选择要被拷贝的文件,谢谢!");
return;
}
if ("".equals(target)) {
JOptionPane.showMessageDialog(null, "请选择目标文件的地址,谢谢!");
return;
}
File srcFile = new File(src);
System.out.println(target+fileName);
File targetFile = new File(target+fileName);
try {
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(targetFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
thread = new Thread(this);
thread.start();
}
if (o == srcButton) { //点击源文件按钮
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); //设置文件选择模式只能打开文件
int result = fileChooser.showOpenDialog(this);
if (result == 0) {
File file = fileChooser.getSelectedFile();
String path = file.getAbsolutePath();
srcTextField.setText(path);
srcTextField.setEditable(false); //设置文本框不可编辑
fileName = file.getName();
}
}
if (o == targetButton) { //点击目标地址按钮
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result = fileChooser.showOpenDialog(this);
if (result == 0) {
File file = fileChooser.getSelectedFile();
String path = file.getAbsolutePath();
targetTextField.setText(path);
targetTextField.setEditable(false);
}
}
}
public void run() {
// TODO Auto-generated method stub
if (progressBarPanel != null) {
try {
byte[] b = new byte[1024];
int size = fis.available();
size = size - (size % 1024) + 1024;
float per = (float) (100 / (size / 1024.0));
int i = 0;
CopyEvent e = new CopyEvent();
while (true) {
int len = fis.read(b);
if (len == -1) break;
fos.write(b, 0, len);
e.setValue((int)(per * i));
progressBarPanel.addEvent(e);
i++;
}
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
大家注意:此ContentPanel实现了一个线程,主要是完进度条的滚动效果。
3、创建ProgressBarPanel类,该类继承一个JPanel面板,主要存放进度条组件,同时实现了一个自定义的监听器CopyListener
package javait.org.filecopy;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
public class ProgressBarPanel extends JPanel implements CopyListener {
private JProgressBar bar;
private JLabel label;
public ProgressBarPanel() {
bar = new JProgressBar(1, 100);
bar.setStringPainted(true);
bar.setValue(0);
label = new JLabel("拷贝进度:");
this.add(label);
this.add(bar);
}
public void addEvent(CopyEvent e) { //在计算或者获取进度的拷贝文件进度值
// TODO Auto-generated method stub
/*if (e.getValue() == 100) {
bar.setValue(100);
}
bar.setValue(e.getValue());*/
if ((e.getValue() + 1) == 100) {
bar.setValue(100);
}
bar.setValue(e.getValue() + 1); //设置进度条的百分比
}
}
4、创建自定义一个监听器CopyListener
package javait.org.filecopy;
public interface CopyListener {
public void addEvent(CopyEvent e);
}
5、创建自定义拷贝文件事件
package javait.org.filecopy;
public class CopyEvent {
int value;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
下面为了让大家能够看到上面程序的效果。我给出程序运行的效果图