本文實例講述了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( "m.ythuaji.com.cn - 當前進度指示..." ); // 創建一條垂直進度條 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