⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 application.java~20~

📁 主要是java中进度条的实现. 实现文件复制功能,能显示进度条.
💻 JAVA~20~
字号:
/**
 * <p>Title:文件复制 </p>
 *
 * <p>Description:简单的文件复制 </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company:Ujn </p>
 *
 * @author leexs
 * @version 0.1
 */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.Font;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.io.*;

public class Application extends JFrame{

    String filePath = null;
    String fileName = null;
    boolean bool = false;
    File sourceFile = null;
    File goalFile = null;


    public Application() {
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Application();
    }

    private void jbInit() throws Exception {
        jButton1.addActionListener(new Application_jButton1_actionAdapter(this));
        jLabel4.setText("source file:");
        jLabel4.setBounds(new Rectangle(29, 110, 75, 26));
        jTextField2.setBorder(BorderFactory.createLineBorder(Color.black));
        jTextField2.setEditable(false);
        jTextField2.setText("这里将显示源文件的路径.");
        jTextField2.setBounds(new Rectangle(108, 109, 273, 27));
        jTextField1.setBorder(BorderFactory.createLineBorder(Color.black));
        jButton3.setBounds(new Rectangle(231, 145, 99, 27));
        jButton3.setText("目的地:");
        jButton3.addActionListener(new Application_jButton3_actionAdapter(this));
        jLabel5.setHorizontalAlignment(SwingConstants.RIGHT);
        jLabel5.setText("选择将文件复制到:");
        jLabel5.setBounds(new Rectangle(37, 145, 181, 23));
        jLabel2.setHorizontalAlignment(SwingConstants.RIGHT);
        jButton2.addActionListener(new Application_jButton2_actionAdapter(this));
        this.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
        jLabel1.setFont(new java.awt.Font("宋体", Font.PLAIN, 20));
        jLabel1.setForeground(Color.cyan);
        jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
        jButton1.setBounds(new Rectangle(229, 81, 99, 27));
        jButton1.setFont(new java.awt.Font("隶书", Font.PLAIN, 15));
        jButton1.setForeground(Color.red);
        jButton1.setText("源路径:");
        jLabel2.setText("选择要复制文件的路径:");
        jLabel2.setBounds(new Rectangle(37, 80, 188, 27));
        jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel3.setText("goal file:");
        jLabel3.setBounds(new Rectangle(36, 173, 66, 24));
        jTextField1.setForeground(Color.yellow);
        jTextField1.setEditable(false);
        jTextField1.setText("选择要复制的文件所在目录.点击开始");
        jTextField1.setBounds(new Rectangle(107, 174, 281, 28));
        jButton2.setBounds(new Rectangle(255, 208, 66, 29));
        jButton2.setForeground(Color.blue);
        jButton2.setText("开始");
        jPanel1.add(jLabel1);
        jPanel1.add(jLabel2);
        jPanel1.add(jTextField1);
        jPanel1.add(jButton2);
        jPanel1.add(jLabel4);
        jPanel1.add(jLabel3);
        jPanel1.add(jTextField2);
        jPanel1.add(jButton3);
        jPanel1.add(jLabel5);
        jPanel1.add(jButton1);
        jLabel1.setText("欢迎使用文件复制小工具 by leexs");
        jLabel1.setBounds(new Rectangle(41, 30, 346, 50));
        jPanel1.setLayout(null);
        applicationFrame.add(jPanel1);
        applicationFrame.setLocation(100, 100);
        applicationFrame.setSize(400, 300);
        applicationFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        applicationFrame.setResizable(false);
        applicationFrame.setVisible(true);
    }

    JFrame applicationFrame = new JFrame("简单文件复制");
    JPanel jPanel1 = new JPanel();
    JLabel jLabel1 = new JLabel();
    JButton jButton1 = new JButton();
    JLabel jLabel2 = new JLabel();
    JLabel jLabel3 = new JLabel();
    JTextField jTextField1 = new JTextField();
    JButton jButton2 = new JButton();
    JLabel jLabel4 = new JLabel();
    JTextField jTextField2 = new JTextField();
    JButton jButton3 = new JButton();
    JLabel jLabel5 = new JLabel();
    JFileChooser chooser = new JFileChooser();
    public void jButton1_actionPerformed(ActionEvent e) {
        int temp = chooser.showOpenDialog(this);
        if (temp == chooser.APPROVE_OPTION) {
            filePath = chooser.getSelectedFile().getPath();
            fileName = chooser.getSelectedFile().getName();
            jTextField2.setText(filePath);
            //jTextField2.setText(fileName);
            sourceFile = new File(filePath);
        }
    }

    public void jButton3_actionPerformed(ActionEvent e) {
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int temp = chooser.showOpenDialog(this);
        filePath = chooser.getSelectedFile().getPath();
        if (temp == chooser.APPROVE_OPTION) {
            jTextField1.setText(filePath);
            goalFile = new File(filePath +"//"+ fileName);
        }
    }

    public void jButton2_actionPerformed(ActionEvent e) {
        final JFrame f = new JFrame();
        if (sourceFile == null || goalFile == null) {
            JOptionPane.showMessageDialog(this, "没有选择源文件或目标路径!");
        } else {
            new Thread() {
                public void run() {

                    try {
                        // 打开文件输出流,把InputStream包装在ProgressMonitorInputStream中。
                        //在当前目录中需要放置一个大文件,建议超过50M
                        InputStream in = new FileInputStream(sourceFile);
                        OutputStream out = new FileOutputStream(goalFile);
                        ProgressMonitorInputStream pm =
                                new ProgressMonitorInputStream(f,
                                "Reading a big file", in);
                        // 读取文件,如果总耗时超过2秒,将会自动弹出一个进度监视窗口。
                        // 显示已读取的百分比。
                        int c;
                        while ((c = pm.read()) != -1) {
                            // 处理代码
                            out.write(c);
                        }
                        pm.close();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }
            .start();

        }
        JOptionPane.showMessageDialog(f,"文件已经成功复制完成!");
    }


    class Application_jButton3_actionAdapter implements ActionListener {
        private Application adaptee;
        Application_jButton3_actionAdapter(Application adaptee) {
            this.adaptee = adaptee;
        }

        public void actionPerformed(ActionEvent e) {
            adaptee.jButton3_actionPerformed(e);
        }
    }


    class Application_jButton2_actionAdapter implements ActionListener {
        private Application adaptee;
        Application_jButton2_actionAdapter(Application adaptee) {
            this.adaptee = adaptee;
        }

        public void actionPerformed(ActionEvent e) {
            adaptee.jButton2_actionPerformed(e);
        }
    }


    class Application_jButton1_actionAdapter implements ActionListener {
        private Application adaptee;
        Application_jButton1_actionAdapter(Application adaptee) {
            this.adaptee = adaptee;
        }

        public void actionPerformed(ActionEvent e) {
            adaptee.jButton1_actionPerformed(e);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -