概述
1、利用线程模拟文件复制的进度显示,如下图所示,点击“复制文件”,开始显示进度条,并实时显示复制进度,复制完成后显示“文件复制已完成!”,点击“取消”,取消复制,进度条归零,点击“×”可以退出系统,注意复制按钮和取消按钮在同一时间,只有一个可用。
package com;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class WelcomeJFrame extends JFrame {
public WelcomeJFrame(){
super("实验八多线程 文件复制进度条演示");
this.setBounds(300,240,400,300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new GridLayout(4,1));
JButton jb1 = new JButton();
jb1.setText("确认");
JButton jb2 = new JButton();
jb2.setText("取消");
if(jb2.isEnabled()){
jb1.setEnabled(false);
}
if(jb1.isEnabled()){
jb2.setEnabled(false);
}
this.add(jb1,BorderLayout.NORTH);
this.add(jb2);
JLabel jLabel= new JLabel();
this.add(jLabel);
jLabel.setText("文件复制0%");
final int[] currentProgress = {0};
final JProgressBar progressBar = new JProgressBar();
final int MIN_PROGRESS = 0;
final int MAX_PROGRESS = 100;
Timer timer = new Timer(50, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
currentProgress[0]++;
if (currentProgress[0] > MAX_PROGRESS) {
currentProgress[0] = MIN_PROGRESS;
}
jLabel.setText("文件复制"+currentProgress[0]+"%");
progressBar.setValue(currentProgress[0]);
}
});
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
jb2.setEnabled(true);
jb1.setEnabled(false);
timer.start();
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
jb1.setEnabled(true);
jb2.setEnabled(false);
currentProgress[0]=0;
jLabel.setText("文件复制"+currentProgress[0]+"%");
progressBar.setValue(currentProgress[0]);
timer.start();
timer.stop();
}
});
progressBar.setMinimum(MIN_PROGRESS);
progressBar.setMaximum(MAX_PROGRESS);
progressBar.setStringPainted(true);
progressBar.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
System.out.println("当前进度值: " + progressBar.getValue() + "; " +
"进度百分比: " + progressBar.getPercentComplete());
}
});
progressBar.setValue(currentProgress[0]);
// 添加到内容面板
this.add(progressBar);
this.setVisible(true);
}
public static void main(String[] args) {
new WelcomeJFrame();
CopyFile cf1 = new CopyFile("C:\Users\hx\Videos\COPY\hang1.txt", "C:\Users\hx\Videos\COPY\hang2.txt");
cf1.start();
}
}
package com;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
public class CopyFile extends Thread {
public File older;// 源文件路径
public File newer;// 复制目标路径
public CopyFile(String older, String newer) {
this.older = new File(older);
this.newer = new File(newer);
}
@Override
public void run() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(older);
fos = new FileOutputStream(newer);
byte[] b = new byte[1024];// 声明一个字节数组,每次读取的数据存到该字节数组里
int length = 0;// 返回每次读取的数据长度
long len = older.length();// 获取源文件的长度
double temp = 0;
DecimalFormat df = new DecimalFormat("##%");
while ((length = fis.read(b)) != -1) {
fos.write(b, 0, length);// 把每次读取的内容,输出到目标路径文件中
temp += length;// 把每次读取的数据长度累加
double d = temp / len;// 计算出已经读取的长度占文件总长度的比率
int jd = (int) d;
if (jd % 10 == 0) {
System.out.println(older.getName() + "文件复制了:" + df.format(d));
}
}
System.out.println(older.getName() + "复制完毕!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class WelcomeJFrame extends JFrame {
public WelcomeJFrame(){
super("实验八多线程 文件复制进度条演示");
this.setBounds(300,240,400,300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new GridLayout(4,1));
JButton jb1 = new JButton();
jb1.setText("确认");
JButton jb2 = new JButton();
jb2.setText("取消");
if(jb2.isEnabled()){
jb1.setEnabled(false);
}
if(jb1.isEnabled()){
jb2.setEnabled(false);
}
this.add(jb1,BorderLayout.NORTH);
this.add(jb2);
JLabel jLabel= new JLabel();
this.add(jLabel);
jLabel.setText("文件复制0%");
final int[] currentProgress = {0};
final JProgressBar progressBar = new JProgressBar();
final int MIN_PROGRESS = 0;
final int MAX_PROGRESS = 100;
Timer timer = new Timer(50, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
currentProgress[0]++;
if (currentProgress[0] > MAX_PROGRESS) {
currentProgress[0] = MIN_PROGRESS;
}
jLabel.setText("文件复制"+currentProgress[0]+"%");
progressBar.setValue(currentProgress[0]);
}
});
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
jb2.setEnabled(true);
jb1.setEnabled(false);
timer.start();
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
jb1.setEnabled(true);
jb2.setEnabled(false);
currentProgress[0]=0;
jLabel.setText("文件复制"+currentProgress[0]+"%");
progressBar.setValue(currentProgress[0]);
timer.start();
timer.stop();
}
});
progressBar.setMinimum(MIN_PROGRESS);
progressBar.setMaximum(MAX_PROGRESS);
progressBar.setStringPainted(true);
progressBar.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
System.out.println("当前进度值: " + progressBar.getValue() + "; " +
"进度百分比: " + progressBar.getPercentComplete());
}
});
progressBar.setValue(currentProgress[0]);
// 添加到内容面板
this.add(progressBar);
this.setVisible(true);
}
public static void main(String[] args) {
new WelcomeJFrame();
CopyFile cf1 = new CopyFile("C:\Users\hx\Videos\COPY\hang1.txt", "C:\Users\hx\Videos\COPY\hang2.txt");
cf1.start();
}
}
最后
以上就是闪闪小刺猬为你收集整理的【JAVA】多线程学习的全部内容,希望文章能够帮你解决【JAVA】多线程学习所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复