復(fù)制大容量文件使用進(jìn)度條_第1頁
復(fù)制大容量文件使用進(jìn)度條_第2頁
復(fù)制大容量文件使用進(jìn)度條_第3頁
復(fù)制大容量文件使用進(jìn)度條_第4頁
復(fù)制大容量文件使用進(jìn)度條_第5頁
已閱讀5頁,還剩3頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、Java程序設(shè)計(jì)課程設(shè)計(jì)復(fù)制大容量文件使用進(jìn)度條課程名稱:Java程序設(shè)計(jì)課程設(shè)計(jì)題目名稱:復(fù)制大容量文件使用進(jìn)度條系 別:計(jì)算機(jī)科學(xué)系年級(jí)專業(yè):14級(jí)學(xué) 號(hào):姓名:任課教師:成績:201年月日一、作品簡介復(fù)制大容量文件使用進(jìn)度條的小程序,要求完成的功能如下:用戶單擊“選擇文件”按鈕,打開文件選擇器(如圖2),用戶選擇被復(fù)制文件的源地址;用戶單擊“選擇地址”按鈕,打開文件夾選擇器(如圖3),用戶選擇目標(biāo)地址;用戶單擊“確定復(fù)制”按鈕后(如圖1),程序?qū)崿F(xiàn)自動(dòng)復(fù)制的功能。但若復(fù)制大容量文件,需要較長的時(shí)間,為使用戶了解復(fù)制的進(jìn)度,當(dāng)復(fù)制需要的時(shí)間超過2秒,程序在運(yùn)行復(fù)制功能的提示,自動(dòng)顯示進(jìn)度條

2、。二、設(shè)計(jì)思想這個(gè)程序主要設(shè)計(jì)的窗口有兩個(gè),一個(gè)是操作復(fù)制文件用的界面,還有一個(gè)就是檢測到文件復(fù)制超過兩秒時(shí),跳出來的進(jìn)度條窗口界面??刂谱x取文件的速度是這個(gè)程序的主要難點(diǎn),要實(shí)現(xiàn)控制進(jìn)度條顯示的快慢可以通過每次從文件中讀取的字節(jié)數(shù)進(jìn)行控制。三、設(shè)計(jì)技術(shù)和方法(一)設(shè)計(jì)平臺(tái)1操作系統(tǒng):Windows 7 旗艦版2JDK版本:jdk-7 windows-x643IDE版本:eclipse4.44開發(fā)語言:Java(二)程序設(shè)計(jì)分析和主要步驟1.創(chuàng)建窗體類UserMoniterFrame,該類繼承自JFrame類。2.在該類中添加控件,實(shí)現(xiàn)窗體布局,該窗體中的主要控件及說明:JTextField:

3、pathTextField(顯示要復(fù)制的文件地址),saveTextField(顯示復(fù)制后文件的保存地址)。Jbutton:pathButton(顯示“選擇文件”的按鈕控件),saveButton(顯示“選擇地址”的按鈕控件),copyButton(顯示“確定復(fù)制”的按鈕控件)。3.編寫工具類ProgressMonitorTest,在該類中定義復(fù)制文件時(shí),顯示進(jìn)度條方法useProgressMonitor()。該方法包含一個(gè)JFrame類型的參數(shù),用于指定顯示進(jìn)度條所依賴的窗體:還有兩個(gè)類型的參數(shù),分別用于指定要復(fù)制的文件以及復(fù)制后的文件保存地址。具體代碼如下: public void use

4、ProgressMonitor(JFrame frame, String copyPath, String newPath) try File file = new File(copyPath); / 根據(jù)要復(fù)制的文件創(chuàng)建File對(duì)象 File newFile = new File(newPath); / 根據(jù)復(fù)制后文件的保存地址創(chuàng)建File對(duì)象 FileOutputStream fop = new FileOutputStream(newFile); / 創(chuàng)建FileOutputStream對(duì)象 InputStream in = new FileInputStream(file); / 讀取

5、文件,如果總耗時(shí)超過2秒,將會(huì)自動(dòng)彈出一個(gè)進(jìn)度監(jiān)視窗口。 ProgressMonitorInputStream pm = new ProgressMonitorInputStream( frame, "文件讀取中,請(qǐng)稍后.", in); int c = 0; byte bytes = new byte1024; / 定義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

6、(Exception ex) ex.printStackTrace(); 四、設(shè)計(jì)體會(huì)和感想在課設(shè)過程中超過兩秒讀取進(jìn)度條的代碼讓我琢磨了很久,也查閱了很多資料,相對(duì)于自己的能力,我覺得做這個(gè)代碼明顯有點(diǎn)有心無力,這也暴露出了自己長期在學(xué)習(xí)方法上存在的錯(cuò)誤。我覺得在經(jīng)過這次課程設(shè)計(jì)后,我需要花費(fèi)更多地時(shí)間在計(jì)算機(jī)編碼上,因?yàn)榛A(chǔ)不足,課設(shè)花費(fèi)了我很多的時(shí)間和精力,既然已經(jīng)打算以后在計(jì)算機(jī)領(lǐng)域發(fā)展,我就應(yīng)該對(duì)自己有更高的要求來彌補(bǔ)我的缺點(diǎn),這是我這次課設(shè)過程中逐漸生成的想法。五、源代碼(一)源代碼package 進(jìn)度條;import java.awt.BorderLayout;import ja

7、va.awt.EventQueue;import java.awt.FileDialog;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import javax.swing.JFileChooser;import javax.swing.JLabel;import javax.swing.JTextField;import javax.swing.JButton;import javax.swing.SwingUtilities;import javax.swi

8、ng.UIManager;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.io.FileInputStream;import java.io.*;import javax.swing.JFrame;import javax.swing.ProgressMonitorInputStream;class ProgressMonitorTest public void useProgressMonitor(JFrame frame, String copyPath, String n

9、ewPath) try File file = new File(copyPath); / 根據(jù)要復(fù)制的文件創(chuàng)建File對(duì)象 File newFile = new File(newPath); / 根據(jù)復(fù)制后文件的保存地址創(chuàng)建File對(duì)象 FileOutputStream fop = new FileOutputStream(newFile); / 創(chuàng)建FileOutputStream對(duì)象 InputStream in = new FileInputStream(file); / 讀取文件,如果總耗時(shí)超過2秒,將會(huì)自動(dòng)彈出一個(gè)進(jìn)度監(jiān)視窗口。 ProgressMonitorInputStream

10、 pm = new ProgressMonitorInputStream( frame, "文件讀取中,請(qǐng)稍后.", in); int c = 0; byte bytes = new byte1024; / 定義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(); public class UserM

11、onitorFrame extends JFrame implements Runnable private JPanel contentPane; private JTextField pathTextField; private JTextField saveTextField; /* * Launch the application. */ public static void main(String args) EventQueue.invokeLater(new Runnable() public void run() try UserMonitorFrame frame = new

12、 UserMonitorFrame(); frame.setVisible(true); catch (Exception e) e.printStackTrace(); ); /* * Create the frame. */ public UserMonitorFrame() setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 481, 231); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5); s

13、etContentPane(contentPane); contentPane.setLayout(null); setTitle("在復(fù)制文件時(shí)使用進(jìn)度條"); JPanel panel = new JPanel(); panel.setBounds(0, 0, 465, 262); contentPane.add(panel); panel.setLayout(null); JLabel pathLabel = new JLabel("文件地址:"); pathLabel.setBounds(42, 45, 72, 15); panel.add(pa

14、thLabel); pathTextField = new JTextField(); pathTextField.setBounds(124, 42, 197, 21); panel.add(pathTextField); pathTextField.setColumns(10); JButton pathButton = new JButton("選擇文件"); pathButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) do_pathBu

15、tton_actionPerformed(arg0); ); pathButton.setBounds(341, 41, 93, 23); panel.add(pathButton); JLabel saveLabel = new JLabel("復(fù)制地址:"); saveLabel.setBounds(42, 104, 72, 15); panel.add(saveLabel); saveTextField = new JTextField(); saveTextField.setBounds(124, 101, 197, 21); panel.add(saveTextF

16、ield); saveTextField.setColumns(10); JButton saveButton = new JButton("選擇地址"); saveButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) do_saveButton_actionPerformed(arg0); ); saveButton.setBounds(341, 100, 93, 23); panel.add(saveButton); JButton copy

17、Button = new JButton("確定復(fù)制"); copyButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) do_copyButton_actionPerformed(arg0); ); copyButton.setBounds(169, 145, 93, 23); panel.add(copyButton); / 定義獲取只可以選擇文件夾的選擇框 public JFileChooser getChooser() JFileChoo

18、ser fd = new JFileChooser(); String windows = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" try UIManager.setLookAndFeel(windows); catch (Exception e) e.printStackTrace(); / 設(shè)置見面風(fēng)格 SwingUtilities.updateComponentTreeUI(fd);/ 使設(shè)置的界面風(fēng)格生效 fd.setFileSelectionMode(fd.DIRECTORIES_ONLY); /

19、指示只顯示目錄 fd.showOpenDialog(this); return fd; /選擇要復(fù)制的文件按鈕的單擊事件 protected void do_pathButton_actionPerformed(ActionEvent arg0) java.awt.FileDialog fd=new FileDialog(this); fd.setVisible(true); String filePath = fd.getDirectory()+fd.getFile(); pathTextField.setText(filePath); /選擇保存文件地址的按鈕單擊事件 protected void do_saveButton_actionPerformed(ActionEvent arg0) JFileChooser fd = getChoose

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論