視窗類
package per.experiment5;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Window_time {
JLabel jlabel=new JLabel();
JFrame jframe;
JPanel jpanel;
Graphics g1;
public void win() {
jframe=new JFrame("時鐘");
jframe.setSize(400, 400);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
jlabel.setHorizontalAlignment(JLabel.CENTER);//設定元件字型位置
jlabel.setForeground(Color.white);//設定元件字型顔色
jlabel.setFont(new Font("宋體",Font.BOLD,20));//設定元件字型
jpanel=new JPanel();
jpanel.setLayout(new BorderLayout());
jpanel.setPreferredSize(new Dimension(200,200));
jpanel.setBackground(Color.black);
jpanel.add(jlabel,BorderLayout.PAGE_END);
jframe.add(jpanel,BorderLayout.CENTER);
jframe.setVisible(true);
//!!!此處注意,getGraphics()方法必須是在元件可視化後才會成功,否則會傳回null
//!!!并且g1在此調用,例如使用fillRect()方法會沒有任何反應,查閱資料解釋是也因為調用成功但時間太短,
//!!!系統會不斷調用repaint()方法進行重繪,導緻圖像一閃而過,我的解決方案是将對g1的調用放線上程類的run()方法中進行不斷循環
//!!!但對這種現象的原因還是不太能清楚明白,對解決方法的簡易性也存在質疑,如果有更好的方法望告知
}
}
`
主類調用類
`public class Title1 {
public static void main(String[] args) {
new MyRunnable();
}
線程類:實作時鐘功能
package per.experiment5;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Calendar;
import javax.swing.JPanel;
class MyRunnable implements Runnable{
public Graphics g;
public JPanel jpanel;
public MyRunnable() {
Thread t=new Thread(this);
t.start();
}
public void run() {
Window_time win1=new Window_time();
win1.win();
g=win1.g1;
jpanel=win1.jpanel;
g.setColor(Color.white);
int h_needle,m_needle,s_needle;
while(true) {try {
Thread.sleep(1000);
g.clearRect(0, 0, 1000, 1000);
g.setColor(Color.black);
g.fillRect(0, 0, 1000, 1000);
}catch(Exception e) {
e.getMessage();
}
Calendar calendar=Calendar.getInstance();//擷取日期
h_needle=calendar.get(Calendar.HOUR);//擷取目前小時
m_needle=calendar.get(Calendar.MINUTE);//擷取目前分鐘
s_needle=calendar.get(Calendar.SECOND);//擷取目前秒
//時鐘的圓盤
g.setColor(Color.white);
//12個大的刻度
for(int i=0;i<12;i++) {
int x1=(int)(192+100*Math.cos(Math.PI/6*i));//在Math.cos,sin方法中,Math.PI等于180°
int y1=(int)(142-100*Math.sin(Math.PI/6*i));
if(i%3!=0)
g.fillRect(x1,y1, 4, 4);
else if(i==3)g.fillRect(x1, y1, 4, 8);
else if(i==6)g.fillRect(x1, y1, 8, 4);
else if(i==9)g.fillRect(x1, y1-8, 4, 8);
else g.fillRect(x1-8, y1, 8, 4);
}
//60個小刻度
for(int i=0;i<60;i++) {
int x1=(int)(192+100*Math.cos(Math.PI/30*i));
int y1=(int)(142-100*Math.sin(Math.PI/30*i));
if(i%5!=0)//與大刻度相重時省略
g.fillRect(x1,y1, 2, 2);
}
//時鐘指針
g.setColor(Color.orange);
g.drawLine(192, 142, (int)(192-50*Math.cos(Math.PI/30*h_needle*5+Math.PI/2)), (int)(142-50*Math.sin(Math.PI/30*h_needle*5+Math.PI/2)));//時針指針r設為50
g.setColor(Color.blue);
g.drawLine(192, 142, (int)(192-70*Math.cos(Math.PI/30*m_needle+Math.PI/2)), (int)(142-70*Math.sin(Math.PI/30*m_needle+Math.PI/2)));//分針r設為70
g.setColor(Color.red);
g.drawLine(192, 142, (int)(192-80*Math.cos(Math.PI/30*s_needle+Math.PI/2)), (int)(142-80*Math.sin(Math.PI/30*s_needle+Math.PI/2)));//秒針r設為80
//設定JLabel内容
String h_needle_s,m_needle_s,s_needle_s;
if(h_needle<10) { h_needle_s=0+String.valueOf(h_needle);}//當h_needle即小時值小于10時,在前面加上0,即0x格式
else {h_needle_s=String.valueOf(h_needle);}
if(m_needle<10) { m_needle_s=0+String.valueOf(m_needle);}
else { m_needle_s=String.valueOf(m_needle);}
if(s_needle<10) { s_needle_s=0+String.valueOf(s_needle);}
else { s_needle_s=String.valueOf(s_needle);}
win1.jlabel.setText("<html><body>"+" "+h_needle_s +" : "+m_needle_s+" : "+s_needle_s+"<br>"+calendar.get(Calendar.YEAR)+"年 "+
(calendar.get(Calendar.MONTH)+1)+"月 "+calendar.get(Calendar.DAY_OF_MONTH)+"日"+"<br>"+" <body></html>");
//calendar.get(Calendar.MONTH)方法獲得的month是從0開始計數,應+1
//通過 "<html><body>"+s1+"<br>"+s2+"<body></html>",利用"<br>"實作對JLabel中的内容強制換行
}
}
}