JavaFX实现简易时钟效果(一)
#代码知识 发布时间: 2026-01-12
本文实例为大家分享了JavaFX实现简易时钟效果的具体代码,供大家参考,具体内容如下

效果图
用当前时间创建时钟,绘制表盘。
钟表是静止的。让指针动起来,请参照:绘制简易时钟(二)
主函数文件 ShowClock:
package primier;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.shape.Line;
public class ShowClock extends Application {
@Override //Override the start method in the Application class
public void start(Stage primaryStage) {
// 创建时钟面板
ClockPane clock = new ClockPane();
// 当前时间整理为字符串
String timeString = clock.getHour() + ":" + clock.getMinute()
+ ":" + clock.getSecond();
Label lbCurrentTime = new Label(timeString);
BorderPane pane = new BorderPane();
pane.setCenter(clock);
pane.setBottom(lbCurrentTime);
// 将时钟字符串设为靠上居中
BorderPane.setAlignment(lbCurrentTime, Pos.TOP_CENTER);
Scene scene = new Scene(pane, 250,250);
primaryStage.setTitle("Display Clock");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main (String[] args) {
Application.launch(args);
}
}
ClockPane 类
package primier;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
public class ClockPane extends Pane {
private int hour;
private int minute;
private int second;
// 时钟面板的宽度和高度
private double w = 250, h = 250;
/** 用当前时间创建时钟 */
public ClockPane() {
setCurrentTime();
}
/** Return hour */
public int getHour() { return hour; }
/** Return minute */
public int getMinute() { return minute; }
/** Return second */
public int getSecond() { return second; }
/** Set the current time for the clock */
public void setCurrentTime() {
// 用当前时间创建Calendar类
Calendar calendar = new GregorianCalendar();
this.hour = calendar.get(Calendar.HOUR_OF_DAY);
this.minute = calendar.get(Calendar.MINUTE);
this.second = calendar.get(Calendar.SECOND);
paintClock();
}
/** 绘制时钟 */
protected void paintClock() {
double clockRadius = Math.min(w,h)*0.4; // 时钟半径
// 时钟中心x, y坐标
double centerX = w/2;
double centerY = h/2;
// 绘制钟表
Circle circle = new Circle(centerX, centerY, clockRadius);
circle.setFill(Color.WHITE); // 填充颜色
circle.setStroke(Color.BLACK); // 笔画颜色
Text t1 = new Text(centerX-5, centerY-clockRadius+12,"12");
Text t2 = new Text(centerX-clockRadius+3, centerY +5, "9");
Text t3 = new Text(centerX+clockRadius-10, centerY+3, "3");
Text t4 = new Text(centerX-3, centerY+clockRadius-3,"6");
// 秒针
double sLength = clockRadius * 0.8;
double secondX = centerX + sLength * Math.sin(second * (2 * Math.PI / 60));
double secondY = centerY - sLength * Math.cos(second * (2 * Math.PI / 60));
Line sLine = new Line(centerX, centerY, secondX, secondY);
sLine.setStroke(Color.GRAY);
// 分针
double mLength = clockRadius * 0.65;
double minuteX = centerX + mLength * Math.sin(minute * (2 * Math.PI / 60));
double minuteY = centerY - mLength * Math.cos(minute * (2 * Math.PI / 60));
Line mLine = new Line(centerX, centerY, minuteX, minuteY);
mLine.setStroke(Color.BLUE);
// 时针
double hLength = clockRadius * 0.5;
double hourX = centerX + hLength *
Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12));
double hourY = centerY - hLength *
Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12));
Line hLine = new Line(centerX, centerY, hourX, hourY);
sLine.setStroke(Color.GREEN);
// 将之前的结点清空,绘制新创建的结点
getChildren().clear();
getChildren().addAll(circle, t1, t2, t3, t4, sLine, mLine, hLine);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
代码知识SEO上一篇 : MyBatis-Plus实现多数据源的示例代码
下一篇 : MyBatis-Plus+Druid配置及应用详解
-
SEO外包最佳选择国内专业的白帽SEO机构,熟知搜索算法,各行业企业站优化策略!
SEO公司
-
可定制SEO优化套餐基于整站优化与品牌搜索展现,定制个性化营销推广方案!
SEO套餐
-
SEO入门教程多年积累SEO实战案例,从新手到专家,从入门到精通,海量的SEO学习资料!
SEO教程
-
SEO项目资源高质量SEO项目资源,稀缺性外链,优质文案代写,老域名提权,云主机相关配置折扣!
SEO资源
-
SEO快速建站快速搭建符合搜索引擎友好的企业网站,协助备案,域名选择,服务器配置等相关服务!
SEO建站
-
快速搜索引擎优化建议没有任何SEO机构,可以承诺搜索引擎排名的具体位置,如果有,那么请您多注意!专业的SEO机构,一般情况下只能确保目标关键词进入到首页或者前几页,如果您有相关问题,欢迎咨询!