窗口类
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);
}
}
`
主类调用类
`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);
for(int i=0;i<12;i++) {
int x1=(int)(192+100*Math.cos(Math.PI/6*i));
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);
}
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)));
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)));
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)));
String h_needle_s,m_needle_s,s_needle_s;
if(h_needle<10) {
h_needle_s=0+String.valueOf(h_needle);}
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(""+" "+h_needle_s +" : "+m_needle_s+" : "+s_needle_s+"
"+calendar.get(Calendar.YEAR)+"年 "+
(calendar.get(Calendar.MONTH)+1)+"月 "+calendar.get(Calendar.DAY_OF_MONTH)+"日"+"
"+" ");
}
}
}