Java电子计时器软件设计报告.docx
《Java电子计时器软件设计报告.docx》由会员分享,可在线阅读,更多相关《Java电子计时器软件设计报告.docx(19页珍藏版)》请在冰豆网上搜索。
![Java电子计时器软件设计报告.docx](https://file1.bdocx.com/fileroot1/2022-12/12/ebe06948-85b7-4d04-94c1-d59e01a7f3d3/ebe06948-85b7-4d04-94c1-d59e01a7f3d31.gif)
Java电子计时器软件设计报告
佛山科学技术学院
《可视化编程技术》课程设计报告
电子计时器软件设计
*******
学号:
**********
年级专业:
11级教育技术学2班
*******
学院:
教育科学学院
广东★佛山
提交日期:
2013年6月
1.前言………………………………………………………………………………2
2.概要设计…………………………………………………………………………3
2.1开发环境……………………………………………………………………3
2.2电子钟计时器功能…………………………………………………………3
2.3界面设计……………………………………………………………………3
2.4类的框架结构图……………………………………………………………4
3.详细设计………………………………………………………………………4
3.1使用的java类的说明……………………………………………………4
3.2类的主要方法………………………………………………………………5
3.2.1时钟钟面类对象设计和按键处理………………………………………6
3.2.2运行处理………………………………………………………………7
3.2.3设计总体认识…………………………………………………………9
3.2.4程序流程图……………………………………………………………9
4.运行结果………………………………………………………………………10
5.测试分析………………………………………………………………………10
5.1程序运行情况……………………………………………………………10
5.2程序异常处理……………………………………………………………11
6.源程序…………………………………………………………………………11
参考文献……………………………………………………………………………20
设计总结……………………………………………………………………………20
摘要:
该程序是一个图形界面的简单的java小时钟嵌套一个简单的秒表,具有良好的、简介的界面。
它是图形界面、线程、流与文件等技术的综合运用,其界面主要采用了java.awt包,javax.swing等。
程序实现了小时钟的基本功能。
可以从电脑上获得准确的背景时间,其时间精确到秒,可以获得准确的星期、日期。
时钟的界面静中带动,秒表跳动。
使用人员能快捷简单地进行操作秒表,即时准确地计算时间,对人们的生活有一定的帮助。
关键字:
java电子计时器,时钟钟面类,方法,事件
1前言
随着科学技术的不断发展,计算机已经成为我们工作学习和生活中不可缺少的工具。
文本编辑器是个人计算机最司空见惯的应用程序了,在学习了Java语言之后,我决定使用Java语言编写一个简单的计算器,可以实现简单的运算功能,满足日常基本的工作学习和娱乐需要。
Java是由Sun公司开发的新一代纯面向对象的网络编程语言。
其目标是建立一种在任意种机器、任一种操作系统的网络环境中运行的软件,实行所谓的“程序写一次,到处运行”的目标。
正因为如此,Java已成为当今Internet上最流行、最受欢迎的一种程序开发语言。
Java开发小组把Java按特性分为基本版、移动版、企业版,每个版本有一个软件开发包。
Java基本版本叫Java2标准版(Java2StandardEdition,J2SE),它包含建立Java应用程序或者是Applet所需的应用程序编程接口(API)。
Java2移动版(TheJava2MobileEdition,J2ME)包含创建无线Java应用程序的API。
还有Java2企业版(TheJava2Enterprise,J2EE)是J2SE的增强版本,包含建立多层架构应用程序API。
Java语言是由C++语言发展起而来的,是一种彻底的面向对象的程序设计语言。
作为一种纯面向对象的程序设计语言,它非常适合大型软件的开发。
Java语言去掉了C++语言的一些容易引起错误的特性。
Java语言的特点有:
面向对象、跨平台、安全性、多线程和图形功能强。
2概要设计
2.1开发环境
开发平台:
MicrosoftWindows7ProfessionalServicePack2
开发工具:
eclipse
2.2计算器功能
功能1:
获得准确的背景时间,这时小时钟最基本的功能。
功能2:
按下“开始”键,开始计时,秒表跳动。
功能3:
按下“停止”键,停止计时,秒表的时间停留在按下“停止”键的那一刻。
功能4:
。
按下“继续”键,秒表继续运行,不会从0开始,富有人性化。
功能5:
按下“重设”键,秒表归零。
2.3界面设计
电子钟和计时器面板
2.4类的框架结构图
classClockPaintextendsJPanelimplementsActionListener,Runnable
publicshizhongandjishiqi()
publicvoidwindowClosing()
publicvoidactionPerformed()
publicvoidrun()
classClockPaintextendsJPanelimplementsRunnable
publicClockPaint()
publicvoidpaint(Graphicsg)
3详细设计
3.1计算器使用的Java类的说明
类名:
shizhongandjishiqi
作用:
主类。
继承的父类:
Frame类
实现的接口:
ActionListener类,Runnable类
publicclassshizhongandjishiqiextendsJFrameimplementsActionListener,Runnable
3.2类的主要方法
3.2.1时钟钟面类对象设计和按键处理:
publicshizhongandjishiqi(intr){//创建时钟钟面类对象
btnStart=newJButton("开始");
btnPause=newJButton("停止");
btnResume=newJButton("继续");
btnStop=newJButton("重设");
lblTime=newJLabel("00:
00:
00.00");
setLayout(null);
btnStart.addActionListener(this);
btnPause.addActionListener(this);
btnResume.addActionListener(this);
btnStop.addActionListener(this);
ClockPaintcp=newClockPaint(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,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(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);//窗口‘关闭’按钮事件处理
}
});
}
按键:
if(btn.getText().equals("开始")){
th=newThread(this);
count=0;
th.start();
}
elseif(btn.getText().equals("停止")){
th.suspend();//因为要继续几时,不能使用stop命令
}
elseif(btn.getText().equals("继续")){
th.resume();//继续
}
elseif(btn.getText().equals("重设")){
lblTime.setText("00:
00:
00:
00");//归零
}
}
publicstaticvoidmain(String[]s){//主程序
newshizhongandjishiqi(100);
}
3.2.2运行处理:
publicvoidrun(){
while(true){
intms,seconds,minutes,hours;
Stringmsg="";
hours=(int)(count/3600000);
minutes=(int)((count-hours*3600000)/60000);
seconds=(int)((count-hours*3600000-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+":
";
}
if(ms<10){
msg+="00"+ms;
}
elseif(ms<100){
msg+="0"+ms;
}
else{
msg+=ms;
}
lblTime.setText(msg);
count++;
try{
Thread.sleep
(1);
}
catch(InterruptedExceptione){
e.printStackTrace();
}
}
}
}
3.2.3设计的总体认识
●JAVA设计的摘要
●JAVA设计的目录结构
●JAVA设计的概要和详细设计
●JAVA设计的版权意识
●JAVA设计需要编译的环境
●JAVA设计的实用性
3.2.4程序流程图
4运行结果
计时器结果显示
5测试分析
5.1程序运行情况:
程序正常的响应按钮事件,在按“开始”键之后,能正常计时,可以正常显示,但如果重新按“开始”键的时候,则会重新计时,其他按键将会失去效果,这个时候就要对结果进行必要的异常处理,但是这个问题还没有得到解决。
5.2程序异常处理:
计时器的异常处理:
有待解决。
时钟的异常处理:
while(true){
try{
Thread.sleep(1000);//休眠一秒
}catch(InterruptedExceptionie){//当程序出现异常,弹出对话框提示
JOptionPane.showMessageDialog(this,ie.getMessage(),"程序异常",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(Graphicsg)
}
6源程序:
package作品;
importjava.awt.*;
importjava.awt.event.*;
importjava.util.*;
importjavax.swing.*;
publicclassshizhongandjishiqiextendsJFrameimplementsActionListener,Runnable{
Threadth;
//JMenuBarmenubar;
//JMenufilemenu;
JButtonbtnStart;
JButtonbtnPause;
JButtonbtnResume;
JButtonbtnStop;
JLabellblTime;
longcount;
publicshizhongandjishiqi(intr){//创建时钟钟面类对象
//menubar=newJMenuBar();
//filemenu=newJMenu("文件");
btnStart=newJButton("开始");
btnPause=newJButton("停止");
btnResume=newJButton("继续");
btnStop=newJButton("重设");
lblTime=newJLabel("00:
00:
00.00");
setLayout(null);
btnStart.addActionListener(this);
btnPause.addActionListener(this);
btnResume.addActionListener(this);
btnStop.addActionListener(this);
ClockPaintcp=newClockPaint(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,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(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);//窗口‘关闭’按钮事件处理
}
});
}
publicvoidactionPerformed(ActionEvente){
JButtonbtn=(JButton)e.getSource();
if(btn.getText().equals("开始")){
th=newThread(this);
count=0;
th.start();
}
elseif(btn.getText().equals("停止")){
th.suspend();
}
elseif(btn.getText().equals("继续")){
th.resume();
}
elseif(btn.getText().equals("重设")){
//th.stop();
lblTime.setText("00:
00:
00:
00");
}
}
publicstaticvoidmain(String[]s){//主程序
newshizhongandjishiqi(100);
}
publicvoidrun(){
while(true){
intms,seconds,minutes,hours;
Stringmsg="";
hours=(int)(count/3600000);
minutes=(int)((count-hours*3600000)/60000);
seconds=(int)((count-hours*3600000-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+":
";
}
if(ms<10){
msg+="00"+ms;
}
elseif(ms<100){
msg+="0"+ms;
}
else{
msg+=ms;
}
lblTime.setText(msg);
count++;
try{
Thread.sleep
(1);
}
catch(InterruptedExceptione){
e.printStackTrace();
}
}
}
}
//该类用于显示时钟钟面
classClockPaintextendsJPanelimplementsRunnable{
intx,y,r;//上边距,左边距,钟面半径
inth,m,s;//时钟,分钟,秒钟在钟面上形成的角度数(与“12:
00:
00”指针形成的夹角)
doublerad=Math.PI/180;//角度1°所对应的弧度
publicClockPaint(intr){//构造函数
this.x=r/2;
this.y=r/2;
this.r=r;
Calendarnow=newGregorianCalendar();//创建当前日期时间对象
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个小时里时钟经过5个刻度点,所以每12分钟才过1刻度点,
//而每个刻度点只见角度相差6度
Threadt=newThread(this);//创建一个线程对象
t.start();//启动线程
}
//方法重写,绘制钟面各元素
publicvoidpaint(Graphicsg){
Graphics2Dg2d=(Graphics2D)g;
//清屏
GradientPaintp=newGradientPaint(120,10,Color.yellow,300,200,Color.blue);
g2d.setPaint(p);
g2d.fillRect(0,0,r*5,r*5);//填充矩形,参数分别为左上角顶点横、纵坐标,宽、高
//画圆
g2d.setColor(Color.white);
//g2d.drawOval(x*2,y*2,r*1,r*1);
g2d.drawOval(x,y,r*2,r*2);//参数分别为矩形的左上角顶点横、纵坐标,矩形宽,矩形高(矩形内切椭圆)
intd=0,x1=0,y1=0;//角度数,横坐标,纵坐标
//刻度点
for(inti=0;i<60;i++){//循环绘出60个刻度点
x1=(int)((r-2)*Math.sin(rad*d));//刻度点横坐标(r-2):
距圆面向内缩进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(inti=0;i<120;i++){//循环绘出60个刻度点
x1=(int)((r-20)*Math.sin(rad*d));//刻度点横坐标(r-2):
距圆面向内缩进2像素画点;