### Java桌面时钟程序知识点解析 #### 一、程序概述 本程序是一个使用Java语言编写的桌面时钟Applet小程序。Applet是一种可以嵌入到网页中的小型应用程序,早期广泛应用于浏览器环境中。此程序主要实现了在桌面上显示当前时间的功能,并能够实时更新。 #### 二、关键组件及功能分析 ##### 1. 导入必要的包 ```java import java.util.*; import java.awt.*; import java.applet.*; import java.text.*; ``` - `java.util.*`: 提供了日期和时间处理类。 - `java.awt.*`: 提供了图形用户界面(GUI)的基本构建块。 - `java.applet.*`: 提供了Applet类及其运行所需的支持类。 - `java.text.*`: 提供了日期格式化类。 ##### 2. 定义`Clock`类 ```java public class Clock extends Applet implements Runnable { // ... } ``` - **继承自`Applet`**: 表明这是一个可以在网页上运行的Applet。 - **实现`Runnable`接口**: 表明该类的对象可以被线程执行,即定时更新时钟显示。 ##### 3. 定义成员变量 ```java private volatile Thread timer; private int lastxs, lastys, lastxm, lastym, lastxh, lastyh; private SimpleDateFormat formatter; private String lastdate; private Font clockFaceFont; private Date currentDate; private Color handColor; private Color numberColor; private int xcenter = 80, ycenter = 55; ``` - **`timer`**: 控制时钟刷新的线程。 - **`lastxs`, `lastys`等**: 用于保存上一次绘制指针的位置。 - **`formatter`**: 日期格式化器,用于格式化日期和时间。 - **`lastdate`**: 保存上一次显示的时间字符串。 - **`clockFaceFont`**: 用于显示数字的字体。 - **`currentDate`**: 当前日期对象。 - **`handColor`**, `numberColor`**: 分别定义指针和数字的颜色。 - **`xcenter`, `ycenter`**: 时钟中心位置。 ##### 4. 初始化方法`init()` ```java public void init() { // 初始化变量 // 设置背景色、字体颜色 // 设置窗口大小 } ``` - **设置背景色**: 通过读取HTML参数来设置背景颜色。 - **初始化字体和颜色**: 根据需要进行初始化。 - **设置窗口大小**: 调用`resize()`方法设置Applet的初始大小为300x300像素。 ##### 5. 更新方法`update()` ```java public void update(Graphics g) { // 获取当前时间 // 格式化时间 // 绘制时钟 } ``` - **获取当前时间**: 使用`new Date()`获取系统当前时间。 - **格式化时间**: 使用`SimpleDateFormat`格式化当前时间。 - **绘制时钟**: 在屏幕上绘制时钟的各个部分。 #### 三、核心逻辑 - **时间更新**: 通过创建一个线程周期性地调用`update()`方法来实现时间的实时更新。 - **绘制时钟**: 根据当前时间计算时针、分针和秒针的位置,并使用`Graphics`对象将其绘制到屏幕上。 - **颜色配置**: 通过HTML参数或默认值设置背景色、指针颜色以及数字颜色。 #### 四、扩展与优化 - **多语言支持**: 可以通过修改`SimpleDateFormat`的构造函数来支持不同的语言和地区。 - **自定义样式**: 允许用户通过HTML参数或配置文件自定义时钟的外观,如指针形状、背景图案等。 - **性能优化**: 对于长时间运行的应用,可以考虑减少不必要的重绘操作,提高程序的性能和响应速度。 通过以上分析可以看出,这个Java桌面时钟程序虽然简单,但涵盖了Applet开发的基础知识,包括GUI组件的绘制、线程管理以及日期时间处理等方面的内容。




























import java.awt.*;
import java.applet.*;
import java.text.*;
/**
* Time!
*
* @author Rachel Gollub
* @modified Daniel Peek replaced circle drawing calculation, few more changes
*/
public class Clock extends Applet implements Runnable {
private volatile Thread timer; // The thread that displays clock
private int lastxs, lastys, lastxm,
lastym, lastxh, lastyh; // Dimensions used to draw hands
private SimpleDateFormat formatter; // Formats the date displayed
private String lastdate; // String to hold date displayed
private Font clockFaceFont; // Font for number display on clock
private Date currentDate; // Used to get date to display
private Color handColor; // Color of main hands and dial
private Color numberColor; // Color of second hand and numbers
private int xcenter = 80, ycenter = 55; // Center position
public void init() {
int x,y;
lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;
formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy",
Locale.getDefault());
currentDate = new Date();
lastdate = formatter.format(currentDate);
handColor = Color.blue;
numberColor = Color.darkGray;
try {
setBackground(new Color(Integer.parseInt(getParameter("bgcolor"),
16)));
} catch (NullPointerException e) {
} catch (NumberFormatException e) {
}
try {
handColor = new Color(Integer.parseInt(getParameter("fgcolor1"),
16));
} catch (NullPointerException e) {
} catch (NumberFormatException e) {
}
try {
numberColor = new Color(Integer.parseInt(getParameter("fgcolor2"),
16));
} catch (NullPointerException e) {
} catch (NumberFormatException e) {
}
resize(300,300); // Set clock window size
}
// Paint is the main part of the program
public void update(Graphics g) {
int xh, yh, xm, ym, xs, ys;
int s = 0, m = 10, h = 10;
String today;
剩余6页未读,继续阅读

- 黎曼の猫2013-11-27可以编译,但用appletviewer查看没反应呀
- lvlyldbfengyue2013-08-27最近正在弄这个,挺有帮助的。

- 粉丝: 0
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 毕业设计三层电梯PLC控制系统设计.doc
- 财务核算软件说明.docx
- autoCADcivil3d测量教程.doc
- 基于项目管理教学的冲压模设计与制造课程改革.doc
- 对人工智能背景下高校法学教育的若干思考.docx
- Thor-AI人工智能资源
- 提高计算机组装与维修教学水平的策略分析.docx
- 电气工程自动化控制的智能化技术应用分析.docx
- 计算机多媒体技术的应用及发展趋势研究.docx
- mapGIS数据中心技术白皮书v.doc
- zino-Rust资源
- 教育技术系3DSMAX课程方案设计书.doc
- photoshop例子制作过程及作业.ppt
- workerman-硬件开发资源
- 应用于入侵检测的机器学习现状与发展分析.docx
- 电子商务专业大专生求职信及自荐信.doc


