java gui界面复制文件到别的目录,并且和进度条保持一致

还有怎么同步实现点击个按钮完成进度条的暂停和继续功能。最好有个完整的代码。... 还有怎么同步实现点击个按钮完成进度条的暂停和继续功能。

最好有个完整的代码。
展开
 我来答
hardneedl
2017-08-28 · TA获得超过921个赞
知道小有建树答主
回答量:1353
采纳率:57%
帮助的人:609万
展开全部
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
展开全部
拷贝时,逐段拷贝文件、更新界面

~
~
~
~
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式