您当前的位置:首页 > 互联网教程

java 闹钟程序

发布时间:2025-05-24 07:53:05    发布人:远客网络

java 闹钟程序

一、java 闹钟程序

public class AlarmClock extends Applet implements Runnable

Thread timer=null;//创建线程timer

Image clockp,gif1,gif2,clock6,clock7;//clockp:闹钟的外壳,闹铃和报时鸟

int lastxs=0,lastys=0,lastxm=0,lastym=0,lastxh=0,lastyh=0;

Date dummy=new Date();//生成Data对象

GregorianCalendar cal=new GregorianCalendar();

SimpleDateFormat df=new SimpleDateFormat("yyyy MM dd HH:mm:ss");//设置时间格式

String lastdate=df.format(dummy);

Font F=new Font("TimesRoman",Font.PLAIN,14);//设置字体格式

TextField showhour,showmin,showsec,sethour,setmin,setsec;//显示当前时间文本框和定时文本框

Label hlabel1,mlabel1,slabel1,hlabel2,mlabel2,slabel2;//显示时间单位时所用的标签(时、分、秒)

Label info1=new Label("欢迎使用定时提醒闹钟"),info2=new Label("");

Label note1=new Label("当前时间:"),note2=new Label("闹钟设置:");

boolean setalerm=false,clickflag=false;//判断是否响铃和振动

int fixh=0,fixm=0,fixs=0;//记录闹钟的定时

public void init()//初始化方法

int fieldx=50,fieldy1=120,fieldy2=220,fieldw=30,fieldh=20,space=50;//显示时间和定时文本框的定位参数

setLayout(null);//将布局管理器初始化为null

note1.setBounds(30,100,60,20);

note1.setBackground(backcolor);

note1.setForeground(Color.black);

note2.setBounds(30,180,60,20);

note2.setBackground(backcolor);

note2.setForeground(Color.black);

showhour=new TextField("00",5);

showmin=new TextField("00",5);

showsec=new TextField("00",5);

//定时用的文本框(时、分、秒)

sethour=new TextField("00",5);

setmin=new TextField("00",5);

setsec=new TextField("00",5);

//当前时间用的文本框的位置、大小

showhour.setBounds(fieldx,fieldy1,fieldw,fieldh);

showhour.setBackground(Color.white);

hlabel1.setBackground(backcolor);

hlabel1.setForeground(Color.black);

hlabel1.setBounds(fieldx+fieldw+3,fieldy1,14,20);

//当前时间的分钟文本框的位置、大小

showmin.setBounds(fieldx,fieldy1,fieldw,fieldh);

showmin.setBackground(Color.white);

mlabel1.setBackground(backcolor);

mlabel1.setForeground(Color.black);

mlabel1.setBounds(fieldx+fieldw+3,fieldy1,14,20);

//当前时间的秒文本框的位置、大小

showsec.setBounds(fieldx,fieldy1,fieldw,fieldh);

showsec.setBackground(Color.white);

slabel1.setBackground(backcolor);

slabel1.setForeground(Color.black);

slabel1.setBounds(fieldx+fieldw+3,fieldy1,14,20);

//定时的小时文本框的位置、大小

sethour.setBounds(fieldx,fieldy2,fieldw,fieldh);

sethour.setBackground(Color.white);

hlabel2.setBackground(backcolor);

hlabel2.setForeground(Color.black);

hlabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);

//定时的分钟文本框的位置、大小

setmin.setBounds(fieldx,fieldy2,fieldw,fieldh);

setmin.setBackground(Color.white);

mlabel2.setBackground(backcolor);

mlabel2.setForeground(Color.black);

mlabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);

setsec.setBounds(fieldx,fieldy2,fieldw,fieldh);

setsec.setBackground(Color.white);

slabel2.setBackground(backcolor);

slabel2.setForeground(Color.black);

slabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);

onbutton.setBounds(90,180,40,20);

offbutton.setBounds(140,180,40,20);

//加入一些附加的信息标签(题头,题尾)

info1.setBackground(backcolor);

info1.setForeground(Color.blue);

info1.setBounds(50,50,150,20);

info2.setBackground(backcolor);

info2.setForeground(Color.blue);

info2.setBounds(150,280,100,20);

//将面板加入当前容器中,并设置面板的大小和背景色

setpanel.setBounds(300,1,250,420);

setpanel.setBackground(backcolor);

ipAu=getAudioClip(getDocumentBase(),"bells/仙剑.mid");

danger=getAudioClip(getDocumentBase(),"bells/0.mid");

chirp=getAudioClip(getDocumentBase(),"bells/3.mid");

s=(int)cal.get(Calendar.SECOND);

m=(int)cal.get(Calendar.MINUTE);

h=(int)cal.get(Calendar.HOUR_OF_DAY);

lastxs=(int)(Math.cos(s*3.14f/30-3.14f/2)*30+xcenter);

lastys=(int)(Math.sin(s*3.14f/30-3.14f/2)*30+ycenter);

lastxm=(int)(Math.cos(m*3.14f/30-3.14f/2)*25+xcenter);

lastym=(int)(Math.sin(m*3.14f/30-3.14f/2)*25+ycenter);

lastxh=(int)(Math.cos((h*30+m/2)*3.14f/180-3.14f/2)*18+xcenter);

lastyh=(int)(Math.sin((h*30+m/2)*3.14f/180-3.14f/2)*18+ycenter);

MediaTracker mt=new MediaTracker(this);//创建Tracke对象

clockp=getImage(getDocumentBase(),"休闲.png");

gif1=getImage(getDocumentBase(),"gif1.gif");

gif2=getImage(getDocumentBase(),"gif2.gif");

clock6=getImage(getDocumentBase(),"clock6.gif");

clock7=getImage(getDocumentBase(),"clock7.gif");

try{mt.waitForAll();}catch(InterruptedException e){};//等待加载结束

resize(600,420);//设置窗口大小

public void paint(Graphics g){//重写paint()方法

int xh,yh,xm,ym,xs,ys,strike_times;

s=(int)cal.get(Calendar.SECOND);

m=(int)cal.get(Calendar.MINUTE);

h=(int)cal.get(Calendar.HOUR_OF_DAY);

xs=(int)(Math.cos(s*3.14f/30-3.14f/2)*30+xcenter);

ys=(int)(Math.sin(s*3.14f/30-3.14f/2)*30+ycenter);

xm=(int)(Math.cos(m*3.14f/30-3.14f/2)*25+xcenter);

ym=(int)(Math.sin(m*3.14f/30-3.14f/2)*25+ycenter);

xh=(int)(Math.cos((h*30+m/2)*3.14f/180-3.14f/2)*18+xcenter);

yh=(int)(Math.sin((h*30+m/2)*3.14f/180-3.14f/2)*18+ycenter);

g.drawImage(clockp,75,110,this);

g.drawImage(clock6,83,280,this);

//以数字方式显示年、月、日和时间

g.drawLine(xcenter,ycenter,xs,ys);

g.drawLine(xcenter,ycenter-1,xm,ym);

g.drawLine(xcenter-1,ycenter,xm,ym);

g.drawLine(xcenter,ycenter-1,xh,yh);

g.drawLine(xcenter-1,ycenter,xh,yh);

if(h<12)hh=h;//将系统时间变换到0-11区间

if(hh==0) strike_times=12;//计算整点时钟声数

if((s==0&&m==0)||flag){//判断是否整点,是否是主动刷新

g.drawImage(gif2,115,35,this);

g.drawImage(gif1,115,35,this);

int timedelta;//记录当前时间与闹铃定时的时差

Integer currh,currm,currs;//分别记录当前的时、分、秒

currh=new Integer(timeNow.getHours());

currm=new Integer(timeNow.getMinutes());

currs=new Integer(timeNow.getSeconds());

//判断是否要更新当前显示的时间,这样可以避免文本框出现频率闪动

if(currh.intValue()!=Integer.valueOf(showhour.getText()).intValue())

showhour.setText(currh.toString());

if(currm.intValue()!=Integer.valueOf(showmin.getText()).intValue())

showmin.setText(currh.toString());

if(currs.intValue()!=Integer.valueOf(showsec.getText()).intValue())

showsec.setText(currh.toString());

if(setalerm){//判断是否设置了闹钟

//判断当前时间是否为闹钟所定的时间

if((currh.intValue()==fixh)&&(currm.intValue()==fixm)&&(currs.intValue()==fixs))

timedelta=currm.intValue()*60+currs.intValue()-fixm*60-fixs;

if((timedelta<60)&&(clickflag==true)){//若当前时间与闹钟相差时间达到60秒

g.drawImage(clock7,83,280,this);

timer=new Thread(this);//将timer实例化

try{timer.sleep(timeout);}catch(InterruptedException e){}

public void update(Graphics g){//采用双缓冲技术的update()方法

offscreen_buf=createImage(600,420);

Graphics offg=offscreen_buf.getGraphics();

ong.drawImage(offscreen_buf,0,0,this);

public boolean action(Event evt,Object arg){//按钮事件处理函数

if(evt.target instanceof Button){

fixh=Integer.valueOf(sethour.getText()).intValue();

fixm=Integer.valueOf(setmin.getText()).intValue();

fixs=Integer.valueOf(setsec.getText()).intValue();

二、求Java万年历源代码!!!

1、你可以把他改下我是没时间帮你该哈!!!

2、function dateSelector()//构造dateSelector对象,用来实现一个日历形式的日期输入框。

3、 this.year=myDate.getFullYear();//定义year属性,年份,默认值为当前系统年份。

4、 this.month=myDate.getMonth()+1;//定义month属性,月份,默认值为当前系统月份。

5、 this.date=myDate.getDate();//定义date属性,日,默认值为当前系统的日。

6、 this.inputName='';//定义inputName属性,即输入框的name,默认值为空。注意:在同一页中出现多个日期输入框,不能有重复的name!

7、 this.display=display;//定义display方法,用来显示日期输入框。

8、function display()//定义dateSelector的display方法,它将实现一个日历形式的日期选择框。

9、 var week=new Array('日','一','二','三','四','五','六');

10、 document.write("<style type=text/css>");

11、 document.write(".ds_font td,span{ font: normal 12px宋体; color:#000000;}");

12、 document.write(".ds_border{ border: 1px solid#000000; cursor: hand; background-color:#DDDDDD}");

13、 document.write(".ds_border2{ border: 1px solid#000000; cursor: hand; background-color:#DDDDDD}");

14、 document.write("</style>");

15、 var M=new String(this.month);

16、 if(M.length==1&&d.length==1){

17、 document.write("<input style='text-align:center;' id='DS_"+this.inputName+"' name='"+this.inputName+"' value='"+this.year+"-0"+this.month+"-0"+this.date+"' title=双击可进行编缉 ondblclick='this.readOnly=false;this.focus()' onblur='this.readOnly=true' readonly>");}

18、 else if(M.length==1&&d.length==2){

19、 document.write("<input style='text-align:center;' id='DS_"+this.inputName+"' name='"+this.inputName+"' value='"+this.year+"-0"+this.month+"-"+this.date+"' title=双击可进行编缉 ondblclick='this.readOnly=false;this.focus()' onblur='this.readOnly=true' readonly>");}

20、 else if(M.length==2&&d.length==1){

21、 document.write("<input style='text-align:center;' id='DS_"+this.inputName+"' name='"+this.inputName+"' value='"+this.year+"-"+this.month+"-0"+this.date+"' title=双击可进行编缉 ondblclick='this.readOnly=false;this.focus()' onblur='this.readOnly=true' readonly>");}

22、 else if(M.length==2&&d.length==2){

23、 document.write("<input style='text-align:center;' id='DS_"+this.inputName+"' name='"+this.inputName+"' value='"+this.year+"-"+this.month+"-"+this.date+"' title=双击可进行编缉 ondblclick='this.readOnly=false;this.focus()' onblur='this.readOnly=true' readonly>");}

24、document.write("<button style='width:60px;height:18px;font-size:12px;margin:1px;border:1px solid#A4B3C8;background-color:#DFE7EF;' type=button onclick=this.nextSibling.style.display='block' onfocus=this.blur()>日期</button>");

25、 document.write("<div style='position:absolute;display:none;text-align:center;width:0px;height:0px;overflow:visible' onselectstart='return false;'>");

26、 document.write("<div style='position:absolute;left:-60px;top:20px;width:142px;height:165px;background-color:#F6F6F6;border:1px solid#245B7D;' class=ds_font>");

27、 document.write("<table cellpadding=0 cellspacing=1 width=140 height=20 bgcolor=#CEDAE7 onmousedown='DS_x=event.x-parentNode.style.pixelLeft;DS_y=event.y-parentNode.style.pixelTop;setCapture();' onmouseup='releaseCapture();' onmousemove='dsMove(this.parentNode)' style='cursor:move;'>");

28、 document.write("<tr align=center>");

29、 document.write("<td width=12% onmouseover=this.className='ds_border' onmouseout=this.className='' onclick=subYear(this) title='减小年份'><<</td>");

30、 document.write("<td width=12% onmouseover=this.className='ds_border' onmouseout=this.className='' onclick=subMonth(this) title='减小月份'><</td>");

31、 document.write("<td width=52%><b>"+this.year+"</b><b>年</b><b>"+this.month+"</b><b>月</b></td>");

32、 document.write("<td width=12% onmouseover=this.className='ds_border' onmouseout=this.className='' onclick=addMonth(this) title='增加月份'>></td>");

33、 document.write("<td width=12% onmouseover=this.className='ds_border' onmouseout=this.className='' onclick=addYear(this) title='增加年份'>>></td>");

34、 document.write("</tr>");

35、 document.write("</table>");

36、 document.write("<table cellpadding=0 cellspacing=0 width=140 height=20 onmousedown='DS_x=event.x-parentNode.style.pixelLeft;DS_y=event.y-parentNode.style.pixelTop;setCapture();' onmouseup='releaseCapture();' onmousemove='dsMove(this.parentNode)' style='cursor:move;'>");

37、 document.write("<tr align=center>");

38、 document.write("<td>"+week[i]+"</td>");

39、 document.write("</tr>");

40、 document.write("</table>");

41、 document.write("<table cellpadding=0 cellspacing=2 width=140 bgcolor=#EEEEEE>");

42、 document.write("<tr align=center>");

43、 document.write("<td width=10% height=16 onmouseover=if(this.innerText!=''&&this.className!='ds_border2')this.className='ds_border' onmouseout=if(this.className!='ds_border2')this.className='' onclick=getValue(this,document.all('DS_"+this.inputName+"'))></td>");

44、 document.write("</tr>");

45、 document.write("</table>");

46、 document.write("<span style=cursor:hand onclick=this.parentNode.parentNode.style.display='none'>【关闭】</span>");

47、 document.write("</div>");

48、 document.write("</div>");

49、 dateShow(document.all("DS_"+this.inputName).nextSibling.nextSibling.childNodes[0].childNodes[2],this.year,this.month)

50、function subYear(obj)//减小年份

51、 var myObj=obj.parentNode.parentNode.parentNode.cells[2].childNodes;

52、 myObj[0].innerHTML=eval(myObj[0].innerHTML)-1;

53、 dateShow(obj.parentNode.parentNode.parentNode.nextSibling.nextSibling,eval(myObj[0].innerHTML),eval(myObj[2].innerHTML))

54、function addYear(obj)//增加年份

55、 var myObj=obj.parentNode.parentNode.parentNode.cells[2].childNodes;

56、 myObj[0].innerHTML=eval(myObj[0].innerHTML)+1;

57、 dateShow(obj.parentNode.parentNode.parentNode.nextSibling.nextSibling,eval(myObj[0].innerHTML),eval(myObj[2].innerHTML))

58、function subMonth(obj)//减小月份

59、 var myObj=obj.parentNode.parentNode.parentNode.cells[2].childNodes;

60、 var month=eval(myObj[2].innerHTML)-1;

61、 dateShow(obj.parentNode.parentNode.parentNode.nextSibling.nextSibling,eval(myObj[0].innerHTML),eval(myObj[2].innerHTML))

62、function addMonth(obj)//增加月份

63、 var myObj=obj.parentNode.parentNode.parentNode.cells[2].childNodes;

64、 var month=eval(myObj[2].innerHTML)+1;

65、 dateShow(obj.parentNode.parentNode.parentNode.nextSibling.nextSibling,eval(myObj[0].innerHTML),eval(myObj[2].innerHTML))

66、function dateShow(obj,year,month)//显示各月份的日

67、 var myDate=new Date(year,month-1,1);

68、 var selectDate=obj.parentNode.parentNode.previousSibling.previousSibling.value.split('-');

69、 if((year%4==0)&&(year%100!=0)||(year%400==0))

70、 for(i=0;i<obj.cells.length;i++)

71、 obj.cells[i].innerHTML='';

72、 obj.cells[i].style.color='';

73、 obj.cells[i].className='';

74、 obj.cells[i+day].innerHTML=(i+1);

75、 if(year==today.getFullYear()&&(month-1)==today.getMonth()&&(i+1)==today.getDate())

76、 obj.cells[i+day].style.color='red';

77、 if(year==eval(selectDate[0])&&month==eval(selectDate[1])&&(i+1)==eval(selectDate[2]))

78、 obj.cells[i+day].className='ds_border2';

79、function getValue(obj,inputObj)//把选择的日期传给输入框

80、 var myObj=inputObj.nextSibling.nextSibling.childNodes[0].childNodes[0].cells[2].childNodes;

81、 if(obj.innerHTML.length==1&&myObj[2].innerHTML.length==1)

82、 inputObj.value=myObj[0].innerHTML+"-0"+myObj[2].innerHTML+"-0"+obj.innerHTML;

83、 else if(obj.innerHTML.length==1&&myObj[2].innerHTML.length==2)

84、 inputObj.value=myObj[0].innerHTML+"-"+myObj[2].innerHTML+"-0"+obj.innerHTML;

85、 else if(obj.innerHTML.length==2&&myObj[2].innerHTML.length==1)

86、 inputObj.value=myObj[0].innerHTML+"-0"+myObj[2].innerHTML+"-"+obj.innerHTML;

87、 else if(obj.innerHTML.length==2&&myObj[2].innerHTML.length==2)

88、 inputObj.value=myObj[0].innerHTML+"-"+myObj[2].innerHTML+"-"+obj.innerHTML;

89、 inputObj.nextSibling.nextSibling.style.display='none';

90、 for(i=0;i<obj.parentNode.parentNode.parentNode.cells.length;i++)

91、 obj.parentNode.parentNode.parentNode.cells[i].className='';

92、 obj.className='ds_border2'

93、function dsMove(obj)//实现层的拖移

94、 obj.style.pixelLeft=X+(event.x-DS_x);

95、 obj.style.pixelTop=Y+(event.y-DS_y);

96、<script language=javascript>

97、 var myDate=new dateSelector();

98、 myDate.inputName='date';//

三、java ~谁能帮我把这段代码写下注释~~

1、*这个类继承了Applet基类,实现了Runable接口。

2、*因为实现该接口,因此必须实现它的run()方法

3、public class Clock extends Applet implements Runnable

4、Thread clockThread;//声明一个线程对象

5、//重载init()方法,它是从applet基类中继承下来的。

6、 font= new Font("TimesRoman",Font.BOLD,48);//对font对象进行实际设置,设为TimesRoman字体,粗体,大小是48

7、//重载start()方法,告知程序在启动时应该做哪些事情。

8、 if(clockThread==null)//看线程引用是否存在,如果还没实例化,则新建一个并赋值

9、 clockThread=new Thread(this,"Showtime");

10、 clockThread.start();//启动线程

11、//实现Runable接口时必须实现它的方法run()

12、//这里的run其实是实现一个帧处理,也就是每1000ms左右为一帧,每帧进行一次屏幕重绘,实现动画效果。

13、//动画是怎么产生的,其实是人眼的视觉暂留机制导致的,不明白可以先搜一下相关问题。

14、repaint();//进行屏幕重绘,默认是刷新整个applet

15、clockThread.sleep(1000);//刷新屏幕后暂停1000毫秒(ms),也就是1秒种

16、catch(InterruptedException e){}//对中断异常进行捕获,处理。

17、public void paint(Graphics g){

18、Date now=new Date();//实例化一个日期对象

19、a=now.getSeconds();//获得当前时间的秒数

20、b=now.getMinutes();//获得当前时间的分钟数

21、c=now.getHours();//获得当前时间的小时数

22、d=(a-15)*Math.PI/180*6;//下面是计算指针对应角度的

23、h=(int)(Math.sin(d)*65);//计算各指针针尖纵横坐标,后面的立即数是他们针的长度。

24、g.translate(100,100);//将坐标的原点转换到(100,100)处,方便绘图。

25、g.drawLine(0,0,i,h);//绘制秒针

26、g.drawLine(0,0,k,j);//绘制分针

27、g.drawLine(0,0,m,l);//绘制时针

28、g.drawString(now.getHours()+":"+now.getMinutes()+":"+now.getSeconds(),-10,100);//绘制HH:MM:SS格式时间字符串。

29、//Applet的stop方法,Applet生命周期的结束。

30、clockThread.stop();//结束时钟这个线程,释放内存资源。

31、*public final void setStub(AppletStub stub)

32、*设置Applet的stub.stub是Java和C之间转换参数并返回值的代码位,它是由系统自动设定的。

33、*public boolean isActive();//判断一个Applet是否处于活动状态。

34、*public URL getDocumentBase();//检索表示该Applet运行的文件目录的对象。

35、*public URL getCodeBase();//获取该Applet代码的URL地址。

36、*public String getParameter(String name);//获取该Applet由name指定参数的值。

37、*public AppletContext getAppletContext();//返回浏览器或小应用程序观察器。

38、*public void resize(int width,int height);//调整Applet运行的窗口尺寸。

39、*public void resize(Dimension d);//调整Applet运行的窗口尺寸。

40、*public void showStatus(String msg);//在浏览器的状态条中显示指定的信息。

41、*public Image getImage(URL url);//按url指定的地址装入图象。

42、*public Image getImage(URL url,String name);//按url指定的地址和文件名加载图像。

43、*public AudioClip getAudioClip(URL url);//按url指定的地址获取声音文件。

44、*public AudioClip getAudioClip(URL url, String name);//按url指定的地址和文件名获取声音。

45、*public String getAppletInfo();//返回Applet应用有关的作者、版本和版权方面的信息;

46、*public String[][] getParameterInfo();

47、//返回描述Applet参数的字符串数组,该数组通常包含三个字符串:参数名、该参数所需值的类型和该参数的说明。

48、*public void play(URL url);//加载并播放一个url指定的音频剪辑。

49、*public void destroy();//撤消Applet及其所占用的资源。若该Applet是活动的,则先终止该Applet的运行。