本文实例讲述了java swing组件实现进度监视功能。分享给大家供大家参考,具体如下:
实例一:
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
|
import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.progressmonitor; import javax.swing.timer; public class testprogressmonitor { timer timer; public void init() { final simulatedtargetmi target = new simulatedtargetmi( 1000 ); // 以启动一条线程的方式来执行一个耗时的任务 final thread targetthread = new thread(target); targetthread.start(); // 创建进度对话框 final progressmonitor dialog = new progressmonitor( null , "等待任务完成,任务完成之前请不要关闭窗口,否则将取消当前操作..." , "已完成:0.00%" , 0 , target.getamount()); // 创建一个计时器 timer = new timer( 300 , new actionlistener() { public void actionperformed(actionevent e) { // 以任务的当前完成量设置进度对话框的完成比例 dialog.setprogress(target.getcurrent()); dialog.setnote( "已完成:" + target.getpercent()); // 如果用户单击了进度对话框的”取消“按钮 if (dialog.iscanceled()) { // 停止计时器 timer.stop(); // 中断任务的执行线程 targetthread.interrupt(); // 系统退出 system.exit( 0 ); } } }); timer.start(); } public static void main(string[] args) { new testprogressmonitor().init(); } } // 模拟一个耗时的任务 class simulatedtargetmi implements runnable { // 任务的当前完成量 private volatile int current; // 总任务量 private int amount; public simulatedtargetmi( int amount) { current = 0 ; this .amount = amount; } public int getamount() { return amount; } public int getcurrent() { return current; } // run方法代表不断完成任务的过程 public void run() { while (current < amount) { try { thread.sleep( 50 ); } catch (interruptedexception e) { } current++; } } public string getpercent() { return string.format( "%.2f" , 100.0 * current / amount) + "%" ; } } |
运行效果:
实例二:
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
|
import java.awt.flowlayout; import java.awt.font; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.windowadapter; import java.awt.event.windowevent; import javax.swing.box; import javax.swing.boxlayout; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jprogressbar; import javax.swing.timer; public class testjprogressbar { jframe frame = new jframe( "www.zzvips.com - 当前进度指示..." ); // 创建一条垂直进度条 jprogressbar bar = new jprogressbar(jprogressbar.horizontal); jlabel tiplabel = new jlabel( "提示:" , jlabel.left); jlabel contentlabel = new jlabel( "任务完成之前请不要关闭窗口,否则将取消当前操作..." , jlabel.left); jlabel statuslabel = new jlabel( " " , jlabel.center); public void init() { frame.setlayout( new flowlayout()); frame.setresizable( false ); tiplabel.setfont( new font( "serif" , font.plain, 14 )); contentlabel.setfont( new font( "serif" , font.plain, 14 )); statuslabel.setfont( new font( "serif" , font.plain, 14 )); jpanel panel = new jpanel(); // fr5.setborder(new titledborder("boxlayout - y")); panel.setlayout( new boxlayout(panel, boxlayout.y_axis)); panel.add(tiplabel); panel.add(box.createverticalstrut( 2 )); panel.add(contentlabel); panel.add(box.createverticalstrut( 7 )); panel.add(bar); // panel.add(box.createverticalglue()); panel.add(box.createverticalstrut( 2 )); panel.add(statuslabel); frame.add(panel, 0 ); final simulatedtarget target = new simulatedtarget( 1000 ); // 以启动一条线程的方式来执行一个耗时的任务 final thread thread = new thread(target); thread.start(); // 设置在进度条中绘制完成百分比 bar.setstringpainted( true ); // bar.setpreferredsize(new dimension(100, 18)); // 设置进度条的最大值和最小值, bar.setminimum( 0 ); // 以总任务量作为进度条的最大值 bar.setmaximum(target.getamount()); final timer timer = new timer( 300 , new actionlistener() { public void actionperformed(actionevent e) { // 以任务的当前完成量设置进度条的value bar.setvalue(target.getcurrent()); if (target.getamount() <= target.getcurrent()) { statuslabel.settext( "处理完成,oh yes!" ); } } }); timer.start(); frame.setlocationrelativeto( null ); frame.setdefaultcloseoperation(jframe.dispose_on_close); // frame.setdefaultcloseoperation(jframe.exit_on_close); frame.addwindowlistener( new windowadapter() { @override public void windowclosing(windowevent e) { thread.interrupt(); timer.stop(); // 系统退出 system.exit( 0 ); } }); // 该代码依据放置的组件设定窗口的大小使之正好能容纳你放置的所有组件 frame.pack(); frame.setvisible( true ); } public static void main(string[] args) { new testjprogressbar().init(); } } // 模拟一个耗时的任务 class simulatedtarget implements runnable { // 任务的当前完成量 private volatile int current; // 总任务量 private int amount; public simulatedtarget( int amount) { current = 0 ; this .amount = amount; } public int getamount() { return amount; } public int getcurrent() { return current; } // run方法代表不断完成任务的过程 public void run() { while (current < amount) { try { thread.sleep( 20 ); } catch (interruptedexception e) { } current++; } } public string getpercent() { return string.format( "%.1f" , 100.0 * current / amount) + "%" ; } } |
运行结果:
希望本文所述对大家java程序设计有所帮助。
原文链接:http://blog.csdn.net/qiuzhi__ke/article/details/49817453