在对大文件操作时,可能会需要些时间,此时为用户提供进度条提示是非常常见的一项功能,这样用户就可以了解操作文件需要的时间信息。本实例为大家介绍了在复制大的文件时使用的进度条提示,需要注意的是,只有在读取文件超过2秒时,才会显示进度条。、
思路分析:
代码如下:
ProgressMonitorTest.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 |
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); // 根据要复制的文件创建File对象 File newFile = new
File(newPath); // 根据复制后文件的保存地址创建File对象 FileOutputStream fop = new
FileOutputStream(newFile); // 创建FileOutputStream对象 InputStream in = new
FileInputStream(file); // 读取文件,如果总耗时超过2秒,将会自动弹出一个进度监视窗口。 ProgressMonitorInputStream pm = new
ProgressMonitorInputStream( frame, "文件读取中,请稍后..." , in); int
c = 0 ; byte [] bytes = new
byte [ 1024 ]; // 定义byte数组 while
((c = pm.read(bytes)) != - 1 ) { // 循环读取文件 fop.write(bytes, 0 , c); // 通过流写数据 } fop.close(); // 关闭输出流 pm.close(); // 关闭输入流 } catch
(Exception ex) { ex.printStackTrace(); } } } |
3. 再看主窗体。JLabel、JTextField神马的就不用说了,选择文件和选择文件夹这两个按钮也是老生常谈了,最重要的是“开始复制”按钮,将由它来负责弹出这个进度条,这就需要开辟一个新的线程,所以主窗体不仅要继承JFrame类,还要实现Runnable接口。他大爷的。
4. 在开始复制按钮的具体方法里,首先创建一个Thread对象作为新的线程,然后调用该对象的start()方法,重载执行run()方法,在该方法中创建一个进度条对象,使用JTextField类的getText()方法获取要复制的文件地址和要复制到的路径,然后调用进度条类里的方法。
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148 |
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()); } //确定复制按钮单击事件 protected
void do_copyButton_actionPerformed(ActionEvent arg0) { Thread thread = new
Thread( this ); thread.start(); } //应用多线程技术实现读取操作 @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())); } } |
效果如图:
Java实现在复制文件时使用进度条,布布扣,bubuko.com
原文:http://www.cnblogs.com/cysolo/p/3575881.html