本文實(shí)例講述了java swing組件實(shí)現(xiàn)進(jìn)度監(jiān)視功能。分享給大家供大家參考,具體如下:
實(shí)例一:
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 ); // 以啟動(dòng)一條線程的方式來執(zhí)行一個(gè)耗時(shí)的任務(wù) final thread targetthread = new thread(target); targetthread.start(); // 創(chuàng)建進(jìn)度對(duì)話框 final progressmonitor dialog = new progressmonitor( null , "等待任務(wù)完成,任務(wù)完成之前請(qǐng)不要關(guān)閉窗口,否則將取消當(dāng)前操作..." , "已完成:0.00%" , 0 , target.getamount()); // 創(chuàng)建一個(gè)計(jì)時(shí)器 timer = new timer( 300 , new actionlistener() { public void actionperformed(actionevent e) { // 以任務(wù)的當(dāng)前完成量設(shè)置進(jìn)度對(duì)話框的完成比例 dialog.setprogress(target.getcurrent()); dialog.setnote( "已完成:" + target.getpercent()); // 如果用戶單擊了進(jìn)度對(duì)話框的”取消“按鈕 if (dialog.iscanceled()) { // 停止計(jì)時(shí)器 timer.stop(); // 中斷任務(wù)的執(zhí)行線程 targetthread.interrupt(); // 系統(tǒng)退出 system.exit( 0 ); } } }); timer.start(); } public static void main(string[] args) { new testprogressmonitor().init(); } } // 模擬一個(gè)耗時(shí)的任務(wù) class simulatedtargetmi implements runnable { // 任務(wù)的當(dāng)前完成量 private volatile int current; // 總?cè)蝿?wù)量 private int amount; public simulatedtargetmi( int amount) { current = 0 ; this .amount = amount; } public int getamount() { return amount; } public int getcurrent() { return current; } // run方法代表不斷完成任務(wù)的過程 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) + "%" ; } } |
運(yùn)行效果:
實(shí)例二:
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.ythuaji.com.cn - 當(dāng)前進(jìn)度指示..." ); // 創(chuàng)建一條垂直進(jìn)度條 jprogressbar bar = new jprogressbar(jprogressbar.horizontal); jlabel tiplabel = new jlabel( "提示:" , jlabel.left); jlabel contentlabel = new jlabel( "任務(wù)完成之前請(qǐng)不要關(guān)閉窗口,否則將取消當(dāng)前操作..." , 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 ); // 以啟動(dòng)一條線程的方式來執(zhí)行一個(gè)耗時(shí)的任務(wù) final thread thread = new thread(target); thread.start(); // 設(shè)置在進(jìn)度條中繪制完成百分比 bar.setstringpainted( true ); // bar.setpreferredsize(new dimension(100, 18)); // 設(shè)置進(jìn)度條的最大值和最小值, bar.setminimum( 0 ); // 以總?cè)蝿?wù)量作為進(jìn)度條的最大值 bar.setmaximum(target.getamount()); final timer timer = new timer( 300 , new actionlistener() { public void actionperformed(actionevent e) { // 以任務(wù)的當(dāng)前完成量設(shè)置進(jìn)度條的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(); // 系統(tǒng)退出 system.exit( 0 ); } }); // 該代碼依據(jù)放置的組件設(shè)定窗口的大小使之正好能容納你放置的所有組件 frame.pack(); frame.setvisible( true ); } public static void main(string[] args) { new testjprogressbar().init(); } } // 模擬一個(gè)耗時(shí)的任務(wù) class simulatedtarget implements runnable { // 任務(wù)的當(dāng)前完成量 private volatile int current; // 總?cè)蝿?wù)量 private int amount; public simulatedtarget( int amount) { current = 0 ; this .amount = amount; } public int getamount() { return amount; } public int getcurrent() { return current; } // run方法代表不斷完成任務(wù)的過程 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) + "%" ; } } |
運(yùn)行結(jié)果:
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
原文鏈接:http://blog.csdn.net/qiuzhi__ke/article/details/49817453