java gui界面复制文件到别的目录,并且和进度条保持一致
2个回答
展开全部
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.nio.file.*;
/**
* 格子绘图演示
* @author hardneedl
*/
final class CopyDemo extends JFrame{
private static final Dimension SIZE = new Dimension(600,400);
public Dimension getMinimumSize() {return SIZE;}
public Dimension getMaximumSize() {return SIZE;}
public Dimension getPreferredSize() {return SIZE;}
private JProgressBar progressBar;
private JTextField statusBar;
private JButton srcBtn,targetBtn,performBtn;
private File srcDir, targetDir;
private SwingWorker copyWorker;
public String getTitle() {return "CopyDemo";}
private class CopyWorker extends SwingWorker<Integer,Integer> {
private JProgressBar bar;
private JTextComponent status;
CopyWorker(JProgressBar bar, JTextComponent status) {
this.bar=bar;
this.status=status;
addPropertyChangeListener(evt -> {
if (evt.getPropertyName().equals("progress")) {
bar.setValue((Integer) evt.getNewValue());
}
});
}
protected Integer doInBackground() throws Exception {
if (srcDir==null){
status.setText("请选源路径");
return 0;
}
if (targetDir==null){
status.setText("请选目标路径");
return 0;
}
File[] files = srcDir.listFiles();
bar.setMaximum(files.length);
bar.setMinimum(0);
int i = 0;
for (File f : files) {
try {
Files.copy(f.toPath(), new File(targetDir, f.getName()).toPath(),StandardCopyOption.REPLACE_EXISTING);
}catch (IOException e) {
e.printStackTrace();
}
i++;
publish(i);
setProgress(i);
status.setText(f.getCanonicalPath());
}
return i;
}
}
private CopyDemo() throws HeadlessException {
super();
init();
addListeners();
doLay();
}
private void init() {
progressBar = new JProgressBar();
statusBar = new JTextField();
srcBtn = new JButton("源路径...");
targetBtn = new JButton("目标路径...");
performBtn =new JButton("复制"){
public Color getForeground() {
return Color.BLUE;
}
};
copyWorker=new CopyWorker(progressBar,statusBar);
}
private void doLay(){
Container container = getContentPane();
container.setLayout(new GridLayout(0,1));
container.add(srcBtn);
container.add(targetBtn);
container.add(performBtn);
container.add(progressBar);
container.add(statusBar);
pack();
setVisible(true);
}
private void addListeners(){
targetBtn.addActionListener(new AbstractAction() {
private JFileChooser fileChooser ;
public void actionPerformed(ActionEvent e) {
if (fileChooser == null) {
fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
fileChooser.setFileFilter(new FileFilter() {
public boolean accept(File f) {
return f.isDirectory();
}
public String getDescription() {
return "目标路径";
}
});
}
if (fileChooser.showOpenDialog(CopyDemo.this) == JFileChooser.APPROVE_OPTION) {
targetDir = fileChooser.getSelectedFile();
}
}
});
srcBtn.addActionListener(new AbstractAction() {
private JFileChooser fileChooser;
public void actionPerformed(ActionEvent e) {
if (fileChooser==null){
fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
fileChooser.setFileFilter(new FileFilter() {
public boolean accept(File f) {
return f.isDirectory();
}
public String getDescription() {
return "源路径";
}
});
}
if(fileChooser.showOpenDialog(CopyDemo.this)==JFileChooser.APPROVE_OPTION)
srcDir=fileChooser.getSelectedFile();
}
});
performBtn.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
copyWorker.execute();
}
});
}
public static void main(String... args) {
System.setProperty("swing.defaultlaf","com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.invokeLater(CopyDemo::new);
}
}
2017-08-25
展开全部
拷贝时,逐段拷贝文件、更新界面
~
~
~
~
~
~
~
~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询