模擬一個電子時鐘,它可以在任何時候被啟動或者停止,并可以獨立的運行。
1.定義一個Clock類。它繼承Label類,并實現(xiàn)Runnable接口。這個類中有一個Thread類型的clocker域,以及start()和run()方法。在run()方法中,每隔一秒就把系統(tǒng)時間顯示為label的文本。
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
|
class Clock extends Label implements Runnable { //定義Thread類型的clocker域 public Thread clocker= null ; public Clock() { //初始化時,把label設(shè)置為當前系統(tǒng)時間 //調(diào)用toString方法轉(zhuǎn)化為String類型 setText( new Date().toString()); } //控制線程的啟動 public void start() { if (clocker== null ) { //clocker通過Thread類構(gòu)造方法得到的對象進行初始化,并將Clock類的當前對象作為參數(shù) clocker= new Thread( this ); clocker.start(); } } //控制線程的停止 public void stop() { clocker= null ; } //實現(xiàn)Runnable接口中的run()方法 public void run() { Thread currentThread=Thread.currentThread(); //判斷clocker是否是當前運行的線程 while (clocker==currentThread) { setText( new Date().toString()); try { //休眠1s鐘 clocker.sleep( 1000 ); } catch (InterruptedException ie) { System.out.println( "Thread error:" +ie); } } } } |
2.定義一個ClockFrame類。它繼承Frame類,并實現(xiàn)ActionListener接口。在這個類中定義start和stop按鈕來控制電子時鐘的運行。并且這個類有一個Clock類的域,把這個Clock類對象添加到ClockFrame類中顯示。
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
|
public class ClockFrame extends Frame implements ActionListener { private Button start= new Button( "Start" ); private Button stop= new Button( "Stop" ); private Clock c= new Clock(); public ClockFrame() { super ( "電子時鐘" ); //設(shè)置窗體使用流式布局 setLayout( new FlowLayout()); //添加按鈕并且為其注冊監(jiān)聽器 add(start); start.addActionListener( this ); add(stop); stop.addActionListener( this ); //使用繼承WindowAdapter的匿名內(nèi)部類來實現(xiàn)窗口的關(guān)閉 addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent we) {System.exit( 0 );} }); add(c); //使構(gòu)件在窗口中得到合理的安排。 pack(); setVisible( true ); } //通過調(diào)用Clock對象中的方法,實現(xiàn)對事件的響應(yīng)。 public void actionPerformed(ActionEvent ae) { if (ae.getSource()==start) { c.start(); } else if (ae.getSource()==stop) c.stop(); } public static void main(String[] args) { new ClockFrame(); } } |
3、運行:
注:
需要導(dǎo)入的類:
1
2
3
|
import java.awt.*; import java.awt.event.*; import java.util.Date; |
再給大家一個網(wǎng)友的做法,非常不錯
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
|
import java.awt.*; import javax.swing.*; import java.util.Date; /*TimeDemo.java * @src public class TimeDemo extends JFrame implements Runnable { Thread clock; public static void main(String[] args) { TimeDemo td = new TimeDemo(); td.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //點擊可見窗口右上角的紅叉關(guān)閉 } public TimeDemo(){ super ( "雪地漫步---java多線程數(shù)字時鐘" ); //繼承父類構(gòu)造方法,這里相當于Font("雪地漫步---java多線程數(shù)字時鐘"); setTitle( "kk" ); //這個則是子類的 this .setFont( new Font( "Times New Roman" ,Font.BOLD, 60 )); //設(shè)置字體大小 this .go(); //自定義go方法,用于以后開啟線程 setBounds( 400 , 300 , 300 , 100 ); this .setVisible( true ); } public void go(){ stop(); if (clock== null ){ //線程執(zhí)行的主題作為Thread類構(gòu)造方法的參數(shù)。 clock= new Thread( this ); clock.start(); //開啟線程,實現(xiàn)run方法 } } public void run() { while ( true ) //讓線程一直進行 { //repain()方法是來控制Graphics類的paint()方法的,repain()方法執(zhí)行一次,即讓paint()方法執(zhí)行一次 repaint(); try { Thread.sleep( 1000 ); //參數(shù)是毫秒,1秒即1000毫秒 } catch (InterruptedException e){} } } public void stop(){ clock= null ; } public void paint(Graphics g){ String s= "" ; Date now= new Date(); int hour=now.getHours(); int minute=now.getMinutes(); int second=now.getSeconds(); s=hour+ ":" +minute+ ":" +second; g.setColor(Color.green); Dimension dim=getSize(); g.fillRect( 0 , 0 , dim.width, dim.height); g.setColor(Color.red); g.drawString(s, 20 , 80 ); } } |
例子三:思路更加的巧妙,分享給大家
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
|
import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.sql.Date; import java.text.SimpleDateFormat; import java.util.Calendar; import javax.swing.JFrame; import javax.swing.JPanel; class Clock extends Canvas implements Runnable{ /* http://ohgrateboy.blog.163.com我的博客 */ private static final long serialVersionUID = 3660124045489727166L; Thread t; JFrame frame= new JFrame(); JPanel conPane; String time; int i= 0 ; Date timer; public Clock(){ conPane=(JPanel)frame.getContentPane(); conPane.setLayout( new BorderLayout()); conPane.setSize( 280 , 40 ); conPane.setBackground(Color.white); conPane.add( this ,BorderLayout.CENTER); t= new Thread( this ); //實例化線 t.start(); //調(diào)用線程 frame.setVisible( true ); frame.setSize( 300 , 150 ); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void run(){ while ( true ){ try { Thread.sleep( 1000 ); //休眠1秒鐘 } catch (InterruptedException e){ System.out.println( "異常" ); } this .repaint( 100 ); } } public void paint(Graphics g){ Font f= new Font( "宋體" ,Font.BOLD, 16 ); SimpleDateFormat SDF= new SimpleDateFormat( "yyyy'年'MM'月'dd'日'HH:mm:ss" ); //格式化時間顯示類型 Calendar now=Calendar.getInstance(); time=SDF.format(now.getTime()); //得到當前日期和時間 g.setFont(f); g.setColor(Color.orange); g.drawString(time, 45 , 25 ); } public static void main(String args[]){ new Clock(); } } |