public void actionPerformed(ActionEvent evt){
//获取当前时间
if(nowh==i && nows==0 && nowm==0){
Calendar now=Calendar.getInstance() ; 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);
for(int i=0;i<=23;i++){
//System.out.println(\ try { } }
StringBuffer s=new StringBuffer(\s.append(Integer.toString(i)); s.append(\//System.out.println(s); String st=new String(s); File f = new File(st); URL cb= f.toURL(); AudioClip aau;
aau = Applet.newAudioClip(cb);
aau.play();
} catch (MalformedURLException e) { }
}
};
new Timer(delay1,drawClock1).start(); } }
这里主要用来在创建时钟主窗口以及为给是添加监视器啊使其能够完成整点报时的功能。
DrawClock.java
import javax.swing.*; import java.awt.*;
import java.util.Calendar;
import java.util.GregorianCalendar; import java.io.*; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.util.Date;
public class DrawClock extends JPanel{
//参数定义区 int
x,y,x0,y0,r,h,w,olds_x=-100,olds_y,oldm_x,oldm_y,oldh_x,oldh_y,hh,old_h;//x0,y0圆心
double ang,mm,ss,old_m;
final double RAD=Math.PI/180;//定义弧度、角度转换常数 JButton set;//\设置\按钮 Icon icon;//按钮图片
//DrawClock构造方法 public DrawClock(){
java.awt.event.ActionListener; import
java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import
this.setLayout(null);//设置布局为空 icon=new ImageIcon(\ set=new JButton(icon);
set.setMnemonic(KeyEvent.VK_ALT);//设置按钮快捷键为:“Alt” set.setSize(30,30);//按钮大小
add(set);
set.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ try {
Process p=Runtime.getRuntime().exec( \//调用Windows时间设置
//按钮添加监听
Timedate.cpl\
}
catch(Exception m){}
}
});
//添加计时器 时间间隔为0.1s int delay = 100;
ActionListener drawClock = new ActionListener(){ public void actionPerformed(ActionEvent evt){
new Timer(delay,drawClock).start(); }
//绘制图像
public void paint(Graphics g){
//参数定义区
Graphics2D g2D = (Graphics2D)g;
repaint();//重画界面
} };
//L和T分别是窗口边缘宽度的一半 h = getSize().height;//h为窗口高度 w = getSize().width; g2D.setColor(Color.white); //设置前景色
r=h/2-30;//计算半径r的值 x0=w/2+35;
y0=h/2+10; //计算圆心坐标 int fs = r/7; //计算字体大小 //调整窗口大小使合适
//获取当前时间
Calendar now=Calendar.getInstance() ; 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);
//绘制时钟上的60个点
g2D.setStroke(new BasicStroke((1+h/100)/2)); for(int i=1;i<=60;i++){ x=(int)(r*Math.cos(RAD*ang)+x0); y=(int)(r*Math.sin(RAD*ang)+y0); g.setColor(Color.BLACK); g.drawLine(x,y,x,y); ang-=6;
}
//绘制时钟上的12个数
for(int i=12,ang=270;i>=1;i--){ g.setFont(new Font(\Unicode\
x=(int)((r-15)*Math.cos(RAD*ang)+x0);
Sans
相关推荐: