当前位置:文档之家› JAVA课程设计钟表(含代码)

JAVA课程设计钟表(含代码)

Java程序课程设计任务书钟表的设计与开发1、主要内容:创建一个钟表。

借助swing类和接口内部类的实现,在本程序中以实现Runnable接口内部类的形式创建多线程对象。

Runnable接口只定义了一个run()方法,所以调用start和sleep()方法时,必须创建Thread实例化对象。

Interrupt()方法的作用是中断线程。

其作用方式是:多线程对象.interrupt()。

2、具体要求(包括技术要求等):系统的功能要求:1.可以记录时间的钟表。

2.熟悉JAVA中swing的组件运用,基本工具的熟练掌握。

学习并掌握以下技术:Java等。

熟练使用以下开发工具:JCreator + JDK 1.6.0_02 等实现系统上述的功能。

3、进度安排:12月28日~ 12月29日:课程设计选题,查找参考资料12月29日~ 1月2日:完成程序代码的编写1月2日~ 1月3日:系统测试与完善1月4日~ 1月5日:完成课程设计报告,准备答辩4、主要参考文献[1]张帆.Java范例开发大全[M].北京:清华大学出版社,2010:0-831.[2]耿祥义,张跃平.Java大学实用教程[M].北京电子工业出版社,2008:213-216摘要随着经济全球化的发展,推动生活节奏的加快,也给时间赋予了更重要的意义。

基于方便人们更好的掌握时间,我们小组设计出了这个小时钟。

本时钟是一个基于Java语言设计而成的一个小程序,目的是显示时间,并且能调准时钟。

整个程序从符合操作简便、界面友好、灵活使用的要求出发,完成调用、调整的全过程。

本课程设计报告介绍了时钟的构成,论述了目标功能模块;给出了时钟设计的步骤,程序主要所用到的Swing组件以及graphics方法。

关键词:时钟,目录摘要 (II)目录 ......................................................................................................................................................... I II 第1章引言 .. (1)1.1课程设计内容 (1)1.2任务分工 (1)第2章时钟的设计 (2)2.1时钟功能的概述 (2)2.1.1时钟数字显示 (2)2.1.2时钟指针显示 (2)2.1.2时钟的设置 (2)第3章时钟的具体实现 (3)3.1界面设计 (3)3.1.1程序流程图 (3)3.1.1显示数字时钟效果 (3)3.1.2显示指针时钟完全效果图 (4)3.1.3设置窗口效果图 (6)第4章结束语 (8)致谢 (8)附录源代码 (9)第1章引言1.1课程设计内容本时钟编写时用到了Java中的Swing组件以及graphics方法,并具有下列处理功能(1)显示时钟功能显示钟表时间和数字时间(2)状态的可切换通过调整框图的大小,可以在数字时钟和指针时钟之间进行切换。

(3)可随时更改时钟时间可以通过左下方的设置时间,即可更改显示时间及日历。

1.2 任务分工第2章时钟的设计2.1时钟功能的概述本时钟不但可以显示数字时钟和钟表时钟,还可以通过调整框图的大小,可以在数字时钟和指针时钟之间进行切换。

2.1.1时钟数字显示运行程序后,可以直接看到数字型的时钟2.1.2时钟指针显示由于打开程序框的框图太小,无法清晰显示指针,我们将指针放在大框图下显示2.1.2时钟的设置本程序可以通过点击左下标的设置键进行调整校对时钟,同时还可以显示日历项第3章时钟的具体实现3.1界面设计界面设计中有用到swing组件的Button,3.1.1程序流程图开始Main()Clock()repaint()paint()drawclock()3.1.1显示数字时钟效果部分代码://在窗体上显示时间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+57);//在窗体显示秒数3.1.2显示指针时钟完全效果图部分代码://绘制图像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;//h为窗口高度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.black);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;}3.1.3设置窗口效果图部分代码:set.addActionListener(new ActionListener(){//按钮添加监听public void actionPerformed(ActionEvent e){try {Process p=Runtime.getRuntime().exec( "control.exe Timedate.cpl");//调用Windows时间设置}catch(Exception m){}}});第4章结束语本程序基本完成了实验要求包括时钟的显示、数字日期的显示、秒表的设定并能完美的进行时间的任意设定。

在代码的编写过程中错遇到的大困难有两个:一是对于钟表的绘制,开始利用graphics对钟表进行绘制。

至此,程序已能完成各项基本要求的功能,我得意之处在时间的设定,该方法虽然笨,但是很实用!但是不足之处在于秒表停止之后再点击Start不能继续计时,即秒表只能使用一次,要解决这个问题,应该要参考更多线程方面的书本。

为使程序更美观,还可以对界面的背景设置图片,为时钟增加声音,甚至添加闹钟功能,以使程序更完善,使用上更人性化。

致谢附录源代码import javax.swing.*;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.event.KeyEvent;import java.awt.*;import java.util.Calendar;import java.util.GregorianCalendar;import java.io.*;class Clock extends JFrame{//参数定义区intx,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;//按钮图片//Clock构造方法public Clock(){super("时钟");// 窗口标题“时钟”setDefaultCloseOperation(EXIT_ON_CLOSE);//定义窗口“关闭按钮”事件为关闭窗口setSize(250,100);//设置窗口初始大小setBackground(new Color(0,0,0,255));//设置背景颜色为黑色setLocation(300,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(){//按钮添加监听public void actionPerformed(ActionEvent e){try {Process p=Runtime.getRuntime().exec( "control.exe Timedate.cpl");//调用Windows时间设置}catch(Exception m){}}});//添加计时器时间间隔为0.1sint delay = 100;ActionListener drawClock = new ActionListener(){public void actionPerformed(ActionEvent evt){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;//h为窗口高度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.black);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+57);//在窗体显示秒数fs=r/9+17;//计算字体大小g.drawLine(L+120+10,T+10,L+120+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,55));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、PMif(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="Tursday";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 = 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,oldh_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);oldh_x=x;oldh_y=y;old_h=hh;}}//主方法public static void main(String[] args){Clock c = new Clock();//创建一个Clock对象}}。

相关主题