创建一个钟表,借助swing类和接口内部类的实现,主要是swing的使用。下面直接附代码。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Calendar;
import java.util.GregorianCalendar;
class Clock extends JFrame{
//参数定义
int x,y,x0,y0,r,h,w,olds_x=-100,olds_y,oldm_x,oldm_y,oldh_y,old_h;
double ang,mm,ss,hh,old_m;
final double RAD=Math.PI/180;//定义弧度,角度转换常数
JButton set;//设置按钮
Icon icon;//按钮图片
//Clock 构造方法
public Clock(){
super("什么鬼");//窗口标题
setDefaultCloseOperation(EXIT_ON_CLOSE);//定义窗口“关闭按钮”事件为关闭窗口
//setSize(250,150);//设置窗口大小
// setBackground(new Color(125,24,125,255));//设置背景颜色
//setLocation(100,100);
setResizable(true);//窗口大小可调
setVisible(true);//窗口可见
this.setLayout(null);//设置布局为空
icon=new ImageIcon("set.png");
set=new JButton(icon);
set.setMnemonic(KeyEvent.VK_ALT);//设置按钮快捷键为:“Alt”
set.setSize(20,20);//按钮大小
add(set);
set.addActionListener(new ActionListener() {//获取Windows的时间
@Override
public void actionPerformed(ActionEvent e) {
try{
Process p=Runtime.getRuntime().exec("control.exe Timedate.cpl");//调用windows时间
}
catch (Exception m){
}
}
});
//添加计时器 时间间隔为0.1s
int delay=100;
ActionListener drawClock=new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
repaint();//重画界面
}
};
new Timer(delay,drawClock).start();
}
//绘制图像
public void paint(Graphics g){
//参数定义区
Graphics2D g2D=(Graphics2D)g;
Insets insets=getInsets();
int L=insets.left/2,T=insets.top/2;//L和T分别是窗口边缘宽度的一半
h=getSize().height;
w=getSize().width;
g2D.setColor(Color.white);//设置前景色为白色
r=h/2-L-T-5;//计算半径r的值
x0=130+r-5+L;//计算圆心坐标
y0=h/2+T-L;
int fs;//调整窗口大小
if(h<100)
h=100;
this.setSize(x0+r+2*L+5,h);
if (h>150)set.setLocation(2,h-60);
else
set.setLocation(0,350);
//获取当地时间
Calendar now=new GregorianCalendar();
int nowh=now.get(Calendar.HOUR_OF_DAY);
int nowm=now.get(Calendar.MINUTE);
int nows=now.get(Calendar.SECOND);
int noww=now.get(Calendar.DAY_OF_WEEK);
//绘制时钟上的59个点
ang=-90;
if(h<150){
if(nows==0){
g.setColor(Color.pink);
g.fillRect(L+120,T,w-120,h);
}
g2D.setStroke(new BasicStroke(1f));
for(int i=1;i<=nows;i++){
x=(int )((r-3)*Math.cos(RAD*ang)+x0);
y=(int )((r-3)*Math.sin(RAD*ang)+y0);
g.setColor(Color.white);
g.drawLine(x,y,x,y);
ang+=(360.0/59.0);
}
}
//绘制时钟上的60个点
else{
g2D.setStroke(new BasicStroke(1+(h/100)/2));
for(int i=1;i<=60;i++){
x=(int)((r-3)*Math.cos(RAD*ang)+x0);
y=(int)((r-3)*Math.sin(RAD*ang)+y0);
g.setColor(Color.white);
g.drawLine(x,y,x,y);
ang-=6;
}
}
//绘制时钟的12个点
ang=60;
g2D.setStroke(new BasicStroke(1+h/100));
for(int i=1;i<=12;i++){
x=(int)((r)*Math.cos(RAD*ang)+x0);
y=(int) ((r)*Math.sin(RAD*ang)+y0);
g.drawLine(x,y,x,y);
ang-=30;
}
//在窗体显示时间
String st;
if(nowh<10)
st="0"+nowh;
else
st=""+nowh;
if(nows%2==0)
st="";
else
st+=":";
if(nowm<10)
st="0"+nowm;
else
st=""+nowm;
g.setColor(getBackground());
g.fillRect(L,T,120,100);
g.setFont(new Font("Digital",Font.PLAIN,44));
g.setColor(new Color(0,255,0,55));
g.drawString("88:88",L+15,T+57);
g.setColor(Color.white);
g.drawString(st,L=15,T+15);
//在窗体显示秒数
fs=r/9+17;//计算字体大小
g.drawLine(L+120+10,T+10,L+20+10,T+10);
if(nows<10)
st="0"+nows;
else
st=""+nows;
g.setColor(new Color(0,0,0));
g.fillRect(x0-fs/2,2*r/3+y0-3*fs/4,fs,3*fs/4);
g.setFont(new Font("Digital",Font.PLAIN,fs));
g.setColor(new Color(255,255,255,255));
g.drawString("88",x0-fs/2,2*r/3+y0);
g.setColor(Color.white);
g.drawString(st,x0-fs/2,2*r/3+y0);
g.drawLine(L+120+10,T+10,L+120+10,T+10);
//显示AM和PM
if(nowh<12)
st="AM";
else
st="PM";
g.setColor(new Color(0,0,0));
g.fillRect(x0-fs/2,y0-2*r/3,fs,3*fs/4);
g.setFont(new Font("IrisUPC",Font.BOLD,fs));
g.setColor(Color.white);
g.drawString(st,x0-fs/2,y0-2*r/3+3*fs/4);
//显示日期
switch(noww){
case 1:st="Sunday";
break;
case 2:st="Monday";
break;
case 3:st="Tuesday";
break;
case 4:st="Wednesday";
break;
case 5:st="Thursday";
break;
case 6:st="Friday";
break;
case 7:st="Saturday";
break;
}
fs=25;
g.setColor(new Color(0,0,0));
g.fillRect(2*L+15,T+75,fs,3*fs/4);
g.setFont(new Font("Computerfont",Font.BOLD,fs));
g.setColor(Color.white);
g.drawString(st,2*L+15,T+75+fs);
//计算时间与度数的关系
if(h>=150){
ss=90-nows*6;
mm=90-nowm*6-(0.1*nows);
hh=90-nowh*30-nowm/2;
g.setColor(new Color(255,255,255));
g.fillOval(x0-2-h/150,y0-2-h/150,4+h/75,4+h/75);
//擦除秒针
g2D.setStroke(new BasicStroke(1f));
if (olds_x>0){
g.setColor(getBackground());
g.drawLine(x0,y0,olds_x,2*y0-olds_y);
}
else
old_m=mm;
old_h=(int)hh;
}
//绘制秒针
x=(int)(r*0.9*Math.cos(RAD*ss))+x0;
y=(int)(r*0.9*Math.sin(RAD*ss))+y0;
g.setColor(Color.white);
g.drawLine(x0,y0,x,2*y0-y);
olds_x=x;
olds_y=y;
g2D.setStroke(new BasicStroke(1.5f));
//擦除分针
if(old_m!=mm){
g.setColor(getBackground());
g.drawLine(x0,y0,oldm_x,2*y0-oldm_y);
}
//绘制分针
x=(int)(r*0.7*Math.cos(RAD*mm))+x0;
y=(int)(r*0.7*Math.sin(RAD*mm))+y0;
g.setColor(Color.white);
g.drawLine(x0,y0,x,2*y0-y);
oldm_x=x;
oldm_y=y;
old_m=mm;
g2D.setStroke(new BasicStroke(2f));
//擦除时针
if(old_h!=hh){
g.setColor(getBackground());
g.drawLine(x0,y0,oldm_x,2*y0-oldh_y);
}
//绘制时针
x=(int)(r*0.5*Math.cos(RAD*hh))+x0;
y=(int)(r*0.5*Math.sin(RAD*hh))+y0;
g.setColor(Color.white);
g.drawLine(x0,y0,x,2*y0-y);
int oldh_x=x;
int oldh_y=y;
double old_h=hh;
}
public static void main(String[] args) {
Clock c=new Clock();
}
}