Java电子计时器软件设计报告_第1页
Java电子计时器软件设计报告_第2页
Java电子计时器软件设计报告_第3页
Java电子计时器软件设计报告_第4页
Java电子计时器软件设计报告_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 佛山科学技术学院可视化编程技术课程设计报告电子计时器软件设计学生姓名: 李泳东学 号: 年级专业:11级教育技术学2班指导老师: 容汝佳学 院: 教育科学学院广东佛山提交日期:2013年6月目 录1. 前言22概要设计3 2.1 开发环境 3 2.2 电子钟计时器功能 3 2.3 界面设计 3 2.4 类的框架结构图 43. 详细设计 4 3.1 使用的JAVA类的说明 4 3.2 类的主要方法 53.2.1时钟钟面类对象设计和按键处理63.2.2 运行处理 73.2.3 设计总体认识 93.2.4 程序流程图 9 4. 运行结果 105. 测试分析 10 5.1 程序运行情况 10 5.2

2、 程序异常处理 116. 源程序 11参考文献20设计总结20摘要:该程序是一个图形界面的简单的java小时钟嵌套一个简单的秒表,具有良好的、简介的界面。它是图形界面、线程、流与文件等技术的综合运用,其界面主要采用了java.awt包,javax.swing等。程序实现了小时钟的基本功能。可以从电脑上获得准确的背景时间,其时间精确到秒,可以获得准确的星期、日期。时钟的界面静中带动,秒表跳动。使用人员能快捷简单地进行操作秒表,即时准确地计算时间,对人们的生活有一定的帮助。关键字:java电子计时器,时钟钟面类,方法,事件1 前言随着科学技术的不断发展,计算机已经成为我们工作学习和生活中不可缺少的

3、工具。文本编辑器是个人计算机最司空见惯的应用程序了,在学习了Java语言之后,我决定使用Java语言编写一个简单的计算器,可以实现简单的运算功能,满足日常基本的工作学习和娱乐需要。Java是由Sun公司开发的新一代纯面向对象的网络编程语言。其目标是建立一种在任意种机器、任一种操作系统的网络环境中运行的软件,实行所谓的“程序写一次,到处运行”的目标。正因为如此,Java已成为当今Internet上最流行、最受欢迎的一种程序开发语言。Java开发小组把Java按特性分为基本版、移动版、企业版,每个版本有一个软件开发包。Java基本版本叫Java 2标准版(Java 2 Standard Editi

4、on,J2SE),它包含建立Java应用程序或者是Applet所需的应用程序编程接口(API)。Java 2移动版(The Java 2 Mobile Edition,J2ME)包含创建无线Java应用程序的API。还有Java 2企业版(The Java 2 Enterprise,J2EE)是J2SE的增强版本,包含建立多层架构应用程序API。Java语言是由C+语言发展起而来的,是一种彻底的面向对象的程序设计语言。作为一种纯面向对象的程序设计语言,它非常适合大型软件的开发。Java语言去掉了C+语言的一些容易引起错误的特性。Java语言的特点有:面向对象、跨平台、安全性、多线程和图形功能强

5、。2 概要设计2.1开发环境 开发平台:Microsoft Windows 7 Professional Service Pack 2 开发工具:eclipse 2.2计算器功能功能 1:获得准确的背景时间,这时小时钟最基本的功能。 功能 2:按下“开始”键,开始计时,秒表跳动。 功能 3:按下“停止”键,停止计时,秒表的时间停留在按下“停止”键的那一刻。 功能 4:。按下“继续”键,秒表继续运行,不会从0开始,富有人性化。 功能 5:按下“重设”键,秒表归零。2.3界面设计电子钟和计时器面板2.4类的框架结构图class ClockPaint extends JPanel implement

6、s ActionListener,Runnablepublic shizhongandjishiqi() public void windowClosing()public void actionPerformed()public void run() class ClockPaint extends JPanel implements Runnablepublic ClockPaint()public void paint(Graphics g) 3详细设计3.1计算器使用的Java类的说明类名: shizhongandjishiqi 作用: 主类。继承的父类: Frame类实现的接口: A

7、ctionListener类,Runnable类public class shizhongandjishiqi extends JFrame implements ActionListener,Runnable3.2类的主要方法3.2.1时钟钟面类对象设计和按键处理: public shizhongandjishiqi(int r) /创建时钟钟面类对象btnStart = new JButton(开始);btnPause = new JButton(停止);btnResume = new JButton(继续);btnStop = new JButton(重设);lblTime = new

8、JLabel(00:00:00.00);setLayout(null);btnStart.addActionListener(this); btnPause.addActionListener(this); btnResume.addActionListener(this); btnStop.addActionListener(this);ClockPaint cp = new ClockPaint(r); this.add(cp); cp.setBounds(0, 0, r*5, r*3); this.add(btnStart); btnStart.setBounds(10,r*4,80,4

9、0); this.add(btnPause); btnPause.setBounds(135,r*4,80,40); this.add(btnResume); btnResume.setBounds(265,r*4,80,40); this.add(btnStop); btnStop.setBounds(395,r*4,80,40); this.add(lblTime); lblTime.setBounds(210,340,100,50); this.setTitle(Clock2); this.setSize(r*5, r*5); this.setVisible(true); this.se

10、tAlwaysOnTop(true);/设置为窗口总在前面显示 this.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0);/窗口关闭按钮事件处理 ); 按键:if(btn.getText().equals(开始) th = new Thread(this); count = 0; th.start(); else if(btn.getText().equals(停止) th.suspend();/因为要继续几时,不能使用stop命令 else if(btn

11、.getText().equals(继续) th.resume();/继续 else if(btn.getText().equals(重设)lblTime.setText(00:00:00:00);/归零 public static void main(String s) /主程序 new shizhongandjishiqi(100); 3.2.2运行处理:public void run() while(true) int ms, seconds, minutes, hours; String msg = ; hours = (int)(count / ); minutes = (int)(

12、count - hours * ) / 60000); seconds = (int)(count - hours * - minutes * 60000) / 500); ms = (int)(count % 1000); if(hours 10) msg += 0 + hours + :; else msg += hours + :; if(minutes 10) msg += 0 + minutes + :; else msg += minutes + :; if(seconds 10) msg += 0 + seconds + :; else msg += seconds + :; i

13、f(ms 10) msg += 00 + ms; else if(ms 100) msg += 0 + ms; else msg += ms; lblTime.setText(msg); count+; try Thread.sleep(1); catch (InterruptedException e) e.printStackTrace(); 3.2.3设计的总体认识l JAVA设计的摘要l JAVA设计的目录结构l JAVA设计的概要和详细设计l JAVA设计的版权意识l JAVA设计需要编译的环境l JAVA设计的实用性不能重复按3.2.4程序流程图开始重设继续停止4运行结果 计时器结

14、果显示5测试分析5.1程序运行情况: 程序正常的响应按钮事件,在按 “开始”键之后,能正常计时,可以正常显示,但如果重新按“开始”键的时候,则会重新计时,其他按键将会失去效果,这个时候就要对结果进行必要的异常处理, 但是这个问题还没有得到解决。5.2 程序异常处理: 计时器的异常处理:有待解决。时钟的异常处理:while (true) try Thread.sleep(1000);/休眠一秒 catch (InterruptedException ie) /当程序出现异常,弹出对话框提示 JOptionPane.showMessageDialog(this, ie.getMessage(),

15、程序异常, JOptionPane.ERROR_MESSAGE); s += 6;/每过1秒,秒针角度增加6度 if(s%360=0) m += 6;/秒针每走一周,分针角度增加6度 if(m%72=0) h += 6;/当秒针角度为360的倍数(即秒针指向12),且分针角度为72的倍数时,时针角度增加6即走到下一个刻点 this.repaint();/重新绘制钟面,系统自动调用paint(Graphics g) 6 源程序:package 作品;import java.awt.*;import java.awt.event.*;import java.util.*;import javax.

16、swing.*;public class shizhongandjishiqi extends JFrame implements ActionListener,RunnableThread th;/JMenuBar menubar;/JMenu filemenu;JButton btnStart;JButton btnPause;JButton btnResume;JButton btnStop;JLabel lblTime;long count;public shizhongandjishiqi(int r) /创建时钟钟面类对象/menubar = new JMenuBar();/fil

17、emenu = new JMenu(文件);btnStart = new JButton(开始);btnPause = new JButton(停止);btnResume = new JButton(继续);btnStop = new JButton(重设);lblTime = new JLabel(00:00:00.00);setLayout(null);btnStart.addActionListener(this); btnPause.addActionListener(this); btnResume.addActionListener(this); btnStop.addAction

18、Listener(this);ClockPaint cp = new ClockPaint(r); this.add(cp); cp.setBounds(0, 0, r*5, r*3); this.add(btnStart); btnStart.setBounds(10,r*4,80,40); this.add(btnPause); btnPause.setBounds(135,r*4,80,40); this.add(btnResume); btnResume.setBounds(265,r*4,80,40); this.add(btnStop); btnStop.setBounds(395

19、,r*4,80,40); this.add(lblTime); lblTime.setBounds(210,340,100,50); this.setTitle(Clock2); this.setSize(r*5, r*5); this.setVisible(true); this.setAlwaysOnTop(true);/设置为窗口总在前面显示 this.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0);/窗口关闭按钮事件处理 ); public voi

20、d actionPerformed(ActionEvent e) JButton btn = (JButton)e.getSource(); if(btn.getText().equals(开始) th = new Thread(this); count = 0; th.start(); else if(btn.getText().equals(停止) th.suspend(); else if(btn.getText().equals(继续) th.resume(); else if(btn.getText().equals(重设) /th.stop();lblTime.setText(00

21、:00:00:00); public static void main(String s) /主程序 new shizhongandjishiqi(100); public void run() while(true) int ms, seconds, minutes, hours; String msg = ; hours = (int)(count / ); minutes = (int)(count - hours * ) / 60000); seconds = (int)(count - hours * - minutes * 60000) / 500); ms = (int)(cou

22、nt % 1000); if(hours 10) msg += 0 + hours + :; else msg += hours + :; if(minutes 10) msg += 0 + minutes + :; else msg += minutes + :; if(seconds 10) msg += 0 + seconds + :; else msg += seconds + :; if(ms 10) msg += 00 + ms; else if(ms 100) msg += 0 + ms; else msg += ms; lblTime.setText(msg); count+;

23、 try Thread.sleep(1); catch (InterruptedException e) e.printStackTrace(); /该类用于显示时钟钟面class ClockPaint extends JPanel implements Runnable int x, y, r;/上边距,左边距,钟面半径int h, m, s;/ 时钟,分钟,秒钟在钟面上形成的角度数(与“12:00:00”指针形成的夹角)double rad = Math.PI / 180;/角度1所对应的弧度public ClockPaint(int r) /构造函数 this.x = r/2; this

24、.y = r/2; this.r = r; Calendar now = new GregorianCalendar();/创建当前日期时间对象 s = now.get(Calendar.SECOND) * 6;/ 初始化秒钟角度;一圆360度,秒钟转一周60秒,所以每走1秒角度转过6度 m = now.get(Calendar.MINUTE) * 6;/ 同上 h = (now.get(Calendar.HOUR_OF_DAY) - 12) * 30/钟面是12时制的,每1个小时所占的角度是360/12=30度 + now.get(Calendar.MINUTE) / 12 * 6;/ 1个

25、小时里时钟经过5个刻度点,所以每12分钟才过1刻度点, /而每个刻度点只见角度相差6度 Thread t = new Thread(this);/创建一个线程对象 t.start();/启动线程/方法重写,绘制钟面各元素public void paint(Graphics g) Graphics2D g2d = (Graphics2D)g; / 清屏GradientPaint p=new GradientPaint(120,10,Color.yellow,300,200,Color.blue);g2d.setPaint(p);g2d.fillRect(0, 0, r * 5, r * 5);/

26、填充矩形,参数分别为左上角顶点横、纵坐标,宽、高 / 画圆g2d.setColor(Color.white);/g2d.drawOval(x*2, y*2, r * 1, r * 1);g2d.drawOval(x, y, r * 2, r * 2);/参数分别为矩形的左上角顶点横、纵坐标,矩形宽,矩形高(矩形内切椭圆) int d = 0,x1 = 0,y1 = 0;/角度数,横坐标,纵坐标 / 刻度点 for (int i = 0; i 60; i+) /循环绘出60个刻度点 x1 = (int) (r - 2) * Math.sin(rad * d);/刻度点横坐标 (r - 2):距圆

27、面向内缩进2像素画点; y1 = (int) (r - 2) * Math.cos(rad * d);/刻度点纵坐标 (rad * d):表示的是弧度 g2d.drawString(., x + r + x1 - 1, y + r - y1 + 1);/后2个参数表示横纵坐标,即从该坐标处画“.” d += 6;/一个圆360度,每隔6度画一个点,共60个点 for (int i = 0; i 120; i+) /循环绘出60个刻度点 x1 = (int) (r -20) * Math.sin(rad * d);/刻度点横坐标 (r - 2):距圆面向内缩进2像素画点; y1 = (int)

28、(r -20) * Math.cos(rad * d);/刻度点纵坐标 (rad * d):表示的是弧度 g2d.drawString(., x + r + x1 - 1, y + r - y1 + 1);/后2个参数表示横纵坐标,即从该坐标处画“.” d += 3;/一个圆360度,每隔6度画一个点,共60个点 / 数字 g2d.setFont (new Font(Font.SERIF,Font.BOLD,13);/大小 g2d.setColor(Color.white); d = 30;/时钟刻度数从1开始,角度的基线是“00”秒时的秒钟,刻度1的夹角为30 for (int i = 1;

29、 i = 12; i+) x1 = (int) (r -10) * Math.sin(rad * d);/(r - 10): 缩进10像素标刻度数 y1 = (int) (r -10) * Math.cos(rad * d); g2d.drawString(i + , x + r + x1 - 4, y + r - y1 + 5); d += 30; / 时针 g2d.setColor(Color.black); g2d.setStroke(new BasicStroke(3.5f); x1 = (int) (2*r / 5) * Math.sin(rad * h); y1 = (int) (

30、2*r / 5) * Math.cos(rad * h); g2d.drawLine(x + r, y + r, x + r + x1, y + r - y1);/画直线, 前2参数表起点坐标,后2参数表终点坐标 / 分针 g2d.setColor(Color.black); g2d.setStroke(new BasicStroke(2.5f); x1 = (int) (3*r / 5) * Math.sin(rad * m); y1 = (int) (3*r / 5) * Math.cos(rad * m); g2d.drawLine(x + r, y + r, x + r + x1, y

31、 + r - y1); / 秒针 g2d.setColor(Color.RED); g2d.setStroke(new BasicStroke(1.5f); x1 = (int) (4*r / 5) * Math.sin(rad * s); y1 = (int) (4*r / 5) * Math.cos(rad * s); g2d.drawLine(x + r, y + r, x + r + x1, y + r - y1); / 获得时间、星期、日期 Calendar now1 = new GregorianCalendar();/获得当前的日期时间对象 int hour = now1.get

32、(Calendar.HOUR_OF_DAY);/小时,获得当前的时刻 int minute = now1.get(Calendar.MINUTE);/分,同上 int second = now1.get(Calendar.SECOND);/秒 int year = now1.get(Calendar.YEAR);/年 int month = now1.get(Calendar.MONTH)+1;/月 int date = now1.get(Calendar.DAY_OF_MONTH);/日 int week = now1.get(Calendar.DAY_OF_WEEK);/星期 / 星期转换字符串 String weekDay = ; switch(-week) case 1:weekDay=星期一;break; case 2:weekDay=星期二;break; case 3:weekDay=星期三;break; case 4:weekDay=星期四;break; case 5:weekDay=星期五;break; case 6:weekDay=星期六;break; case 7:weekDay=星期日;break; default :weekDay=;break; / 显示时间、星期、日期 g2d.setFont (new Font(Font.SERIF,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论