- 1、本文档共11页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
Java小时钟课程设计
利用JAVA实现一个时钟的小程序
1 设计方法
在设计简单小时钟时,需要编写1个Java源文件:Clock.java
2 程序功能图及程序相关说明
2.1 主功能框
图1 程序基本框图
2.2 程序分析
程序中引入的包
package Clock
import java.awt.Clock //颜色类
import java.awt.Graphics //图形类
import java.awt.event.WindowAdapter //窗口事件适配器
import java.awt.event.WindowEvent //窗口事件类
import java.util.Calendar //日历类
import java.util.GregorianCalendar //使用GregorianCalendar对象的get方(参数)获取**********************************************************
* 类名:Clock *
* 作用:定义小时钟 *
* 继承的父类:JFrame *
**********************************************************
public class clock extends JFrame{
定义的对象:
ClockPaint
size //窗口大小
Resizable //调整窗口大小
Location // 初始位置
Title //窗口标题
Visible //窗口可视化
WindowListener,WindowClosing //创建窗口,并实现关闭功能
定义子类:ClockPaint
继承的父类名:JPanel
继承的接口名:Runnable
class ClockPaint extends JPanel implements Runnable
int h, m, s // 小时,分钟,秒
主要成员方法:
public ClockPaint
this.x = x
this.y = y
this.r = r
s = now.get(Calendar.SECOND) * 6 // 获得秒转换成度数
m = now.get(Calendar.MINUTE) * 6 // 获得分钟
h = (now.get(Calendar.HOUR_OF_DAY) - 12) * 30+ now.get(Calendar.MINUTE) / 12 * 6 // 获得小时
主要成员方法:
public void paint(Graphics g) // 清屏
super.paint(g)
g.setColor(Color.BLACK) //设置底图颜色为黑色
g.fillRect(0, 0, r * 3, r * 3)
// 画圆
g.setColor(Color.WHITE)
g.drawOval(x, y, r * 2, r * 2)
// 秒针
g.setColor(Color.RED)
int x1 = (int) ((r - 10) * Math.sin(rad * s))
int y1 = (int) ((r - 10) * Math.cos(rad * s))
g.drawLine(x + r, y + r, x + r + x1, y + r - y1)
// 分针
g.setColor(Color.BLUE)
x1 = (int) ((r - r / 2.5) * Math.sin(rad * m))
y1 = (int) ((r - r / 2.5) * Math.cos(rad * m))
g.drawLine(x + r, y + r, x + r + x1, y + r - y1)
// 时针
文档评论(0)