婷婷久久综合九色综合,欧美成色婷婷在线观看视频,偷窥视频一区,欧美日本一道道一区二区

<tt id="bu9ss"></tt>
  • <span id="bu9ss"></span>
  • <pre id="bu9ss"><tt id="bu9ss"></tt></pre>
    <label id="bu9ss"></label>

    當前位置:首頁 >  站長 >  編程技術(shù) >  正文

    java進度條代碼分享:實現(xiàn)在復(fù)制文件時使用

     2021-01-07 16:46  來源: 網(wǎng)絡(luò)綜合   我來投稿 撤稿糾錯

      阿里云優(yōu)惠券 先領(lǐng)券再下單

    思路分析:

    因為既要有操作面板又要有進度條,所以肯定要出現(xiàn)兩個繼承JFrame類的窗體。

    先看被調(diào)用的進度條窗體,它不需要手動操作,所以類的內(nèi)部實現(xiàn)一個方法就可以了。因為設(shè)計文件操作,所以要捕獲異常。首先根據(jù)要復(fù)制的文件創(chuàng)建File對象,以及根據(jù)復(fù)制后文件的保存地址創(chuàng)建File對象,然后創(chuàng)建FileOutputStream對象,再創(chuàng)建FileInputStream對象,之后是ProgressMonitorInputStream對象,然后讀取文件,如果總耗時超過2秒,將會自動彈出一個進度監(jiān)視窗口。接下來定義byte數(shù)組,再使用while循環(huán)讀取文件,使用FileOutputStream類的write()方法通過流寫數(shù)據(jù),再使用FileOutputStream類的close()方法關(guān)閉輸出流,最后使用ProgressMonitorInputStream類的close()方法關(guān)閉輸入流。可見該方法需要三個參數(shù):彈出它的父窗口、要復(fù)制的文件地址以及要復(fù)制到的文件夾。

    代碼如下:

    ProgressMonitorTest.java:
    package cn.edu.xidian.crytoll;
    import java.io.FileInputStream;
    import java.io.*;
    import javax.swing.JFrame;
    import javax.swing.ProgressMonitorInputStream;
    public class ProgressMonitorTest {

        public void useProgressMonitor(JFrame frame, String copyPath, String newPath) {
            try {
                File file = new File(copyPath); // 根據(jù)要復(fù)制的文件創(chuàng)建File對象
                File newFile = new File(newPath); // 根據(jù)復(fù)制后文件的保存地址創(chuàng)建File對象
                FileOutputStream fop = new FileOutputStream(newFile); // 創(chuàng)建FileOutputStream對象
                InputStream in = new FileInputStream(file);
                // 讀取文件,如果總耗時超過2秒,將會自動彈出一個進度監(jiān)視窗口。
                ProgressMonitorInputStream pm = new ProgressMonitorInputStream(
                        frame, "文件讀取中,請稍后...", in);
                int c = 0;
                byte[] bytes = new byte[1024]; // 定義byte數(shù)組
                while ((c = pm.read(bytes)) != -1) { // 循環(huán)讀取文件
                    fop.write(bytes, 0, c); // 通過流寫數(shù)據(jù)
                }
                fop.close(); // 關(guān)閉輸出流
                pm.close(); // 關(guān)閉輸入流
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    3. 再看主窗體。JLabel、JTextField神馬的就不用說了,選擇文件和選擇文件夾這兩個按鈕也是老生常談了,最重要的是“開始復(fù)制”按鈕,將由它來負責(zé)彈出這個進度條,這就需要開辟一個新的線程,所以主窗體不僅要繼承JFrame類,還要實現(xiàn)Runnable接口。他大爺?shù)摹?/p>

    4. 在開始復(fù)制按鈕的具體方法里,首先創(chuàng)建一個Thread對象作為新的線程,然后調(diào)用該對象的start()方法,重載執(zhí)行run()方法,在該方法中創(chuàng)建一個進度條對象,使用JTextField類的getText()方法獲取要復(fù)制的文件地址和要復(fù)制到的路徑,然后調(diào)用進度條類里的方法。

    代碼如下:
    package cn.edu.xidian.crytoll;
    import java.awt.BorderLayout;
    import java.awt.Desktop;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;

    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.border.EmptyBorder;
    import javax.swing.filechooser.FileNameExtensionFilter;

    public class UserMonitorFrame extends JFrame implements Runnable{

        /**
         *
         */
        private static final long serialVersionUID = 8674569541853793419L;
        private JPanel contentPane;
        private JTextField fileField;
        private JTextField searchTextField;
        private JTextField replaceTextField;
        private File file;
        private JTextField textField;
        private JTextField textField_1;

        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        UserMonitorFrame frame = new UserMonitorFrame();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        /**
         * Create the frame.
         */
        public UserMonitorFrame() {
            setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 501, 184);
            setTitle("在讀取文件時使用進度條");
            getContentPane().setLayout(null);

            JLabel label = new JLabel("\u6587\u4EF6\u5730\u5740\uFF1A");
            label.setBounds(10, 10, 70, 15);
            getContentPane().add(label);

            textField = new JTextField();
            textField.setBounds(90, 7, 300, 21);
            getContentPane().add(textField);
            textField.setColumns(10);

            JButton button = new JButton("\u9009\u62E9\u6587\u4EF6");
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    do_button_actionPerformed(e);
                }
            });
            button.setBounds(400, 6, 93, 23);
            getContentPane().add(button);

            JLabel label_1 = new JLabel("\u590D\u5236\u5730\u5740\uFF1A");
            label_1.setBounds(10, 40, 70, 15);
            getContentPane().add(label_1);

            textField_1 = new JTextField();
            textField_1.setBounds(90, 38, 300, 21);
            getContentPane().add(textField_1);
            textField_1.setColumns(10);

            JButton button_1 = new JButton("\u9009\u62E9\u5730\u5740");
            button_1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    do_button_1_actionPerformed(e);
                }
            });
            button_1.setBounds(400, 39, 93, 23);
            getContentPane().add(button_1);

            JButton button_2 = new JButton("\u5F00\u59CB\u590D\u5236");
            button_2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    do_copyButton_actionPerformed(e);
                }
            });
            button_2.setBounds(182, 69, 93, 23);
            getContentPane().add(button_2);
        }
        protected void do_button_actionPerformed(ActionEvent e){
            JFileChooser chooser=new JFileChooser();
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            // 顯示文件打開對話框
            int option = chooser.showOpenDialog(this);
            // 確定用戶按下打開按鈕,而非取消按鈕
            if (option != JFileChooser.APPROVE_OPTION)
                return;
            // 獲取用戶選擇的文件對象
            file = chooser.getSelectedFile();
            // 顯示文件信息到文本框
            textField.setText(file.toString());
        }
        protected void do_button_1_actionPerformed(ActionEvent e){
            JFileChooser chooser=new JFileChooser();
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int option=chooser.showOpenDialog(this);
            if(option!=JFileChooser.APPROVE_OPTION)
                return;
            file=chooser.getSelectedFile();
            textField_1.setText(file.toString());
        }
      //確定復(fù)制按鈕單擊事件
        protected void do_copyButton_actionPerformed(ActionEvent arg0) {
           Thread thread = new Thread(this);
           thread.start();
        }
       //應(yīng)用多線程技術(shù)實現(xiàn)讀取操作
        @Override
        public void run() {
            ProgressMonitorTest test = new ProgressMonitorTest();
            String path = textField.getText();
            String save = textField_1.getText();
            test.useProgressMonitor(this,path,save+path.substring(path.lastIndexOf("."),path.length()));

        }
    }

    申請創(chuàng)業(yè)報道,分享創(chuàng)業(yè)好點子。點擊此處,共同探討創(chuàng)業(yè)新機遇!

    相關(guān)標簽
    java進度條

    相關(guān)文章

    熱門排行

    信息推薦