JavaFx是开发Java GUI程序的新框架。JavaFX应用可以无缝地在桌面或web浏览器中运行。具有内建的2D、3D动画支持,以及视频和音频的回放功能,可以作为一个应用独立运行或者在浏览器中运行。

ClockPane类:

import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import java.util.Calendar;
import java.util.GregorianCalendar; public class ClockPane extends Pane{
private int hour;
private int minute;
private int second; //设置时钟面板的宽度和高度
private double w = 250, h = 250; //构造函数
public ClockPane() {
setCurrentTime();
} //设置带参数的构造函数
public ClockPane(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
paintClock();
} //返回小时
public int getHour() {
return hour;
} //返回分钟
public int getMinute() {
return minute;
} //返回秒钟
public int getSecond() {
return second;
} //设置一个新的小时
public void setHour(int hour) {
this.hour = hour;
paintClock();
} //设置一个新的分钟
public void setMinute(int minute) {
this.minute = minute;
paintClock();
} //设置一个新的秒钟
public void setSecond(int second) {
this.second = second;
paintClock();
} //返回时钟面板的宽度
public double getW() {
return w;
} //设置一个新的面板宽度
public void setW(double w) {
this.w = w;
paintClock();
} //返回时钟面板的高度
public double getH() {
return h;
} //设置时钟面板的高度
public void setH(double h) {
this.h = h;
paintClock();
} public void setCurrentTime() {
//获取当前时间
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();
} public void paintClock() {
//初始化时钟参数
double clockRadius = Math.min(w, h) * 0.8 * 0.5;
double centerX = w / 2;
double centerY = h / 2; //显示当前时间,并设置时间显示的位置
String timeString = hour + ":" + minute + ":" + second;
Label labelCurrentTime = new Label(timeString);
labelCurrentTime.setLayoutX(centerX - 25);
labelCurrentTime.setLayoutY(centerY + clockRadius + 5); //画圆
Circle circle = new Circle(centerX, centerY, clockRadius);
//circle.setFill(Color.WHITE);
//circle.setStroke(Color.BLACK);
circle.setStyle("-fx-stroke: black; -fx-fill: white");
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 + 5, "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.0));
double secondY = centerY - sLength * Math.cos(second * (2 * Math.PI / 60.0));
Line sLine = new Line(centerX, centerY, secondX, secondY);
sLine.setStroke(Color.RED); //画分针
double mLength = clockRadius * 0.65; //设置分针的长度
double minuteX = centerX + mLength * Math.sin(minute * 2 * Math.PI / 60.0);
double minuteY = centerY - mLength * Math.cos(minute * 2 * Math.PI / 60.0);
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);
hLine.setStroke(Color.GREEN); getChildren().clear();
getChildren().addAll(circle, t1, t2, t3, t4, sLine, mLine, hLine, labelCurrentTime);
} }

显示时钟程序:

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.util.Duration; public class ClockAniamtion extends Application{ @Override
public void start(Stage primaryStage) throws Exception {
ClockPane clock = new ClockPane(); //将clock放在pane里
BorderPane pane = new BorderPane();
pane.setCenter(clock); //创建一个handler
EventHandler<ActionEvent> eventHandler = e -> {
clock.setCurrentTime();
}; Timeline animation = new Timeline(
new KeyFrame(Duration.millis(1000), eventHandler));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play(); Scene scene= new Scene(pane, 250, 250);
primaryStage.setTitle("时钟");
primaryStage.setScene(scene);
primaryStage.show(); //让clock跟随窗口的变化而变化
pane.widthProperty().addListener(ov -> clock.setW(pane.getWidth()));
pane.heightProperty().addListener(ov -> clock.setH(pane.getHeight()));
} public static void main(String[] args) {
Application.launch(args);
} }

最新文章

  1. Tensorflow学习笔记2:About Session, Graph, Operation and Tensor
  2. [JS] JavaScript框架(1) jQuery
  3. [C#]動態叫用Web Service
  4. IOS 在http请求中使用cookie
  5. ajax同步,异步
  6. nginx模块开发(18)—日志分析
  7. Android 入门第一课 一个简单的提示框
  8. XMPPFrameWork IOS 开发(二)- xcode配置
  9. 高效 Java Web 开发框架 JessMA v3.2.3 beta-1 发布
  10. [Android学习笔记]some tips
  11. JQuery使用mousedown和mouseup简单判断鼠标按下与释放位置是否相同
  12. CentOS 下mysql ERROR&amp;n…
  13. 记一次vscode升级后,格式化Vue出现的问题
  14. LabVIEW(二):计数器应用
  15. shareSdk分享图片
  16. java实现消息队列的两种方式
  17. 交换机的默认网关(跨网段telnet)
  18. u-boot之怎么实现分区
  19. 创建Filter类
  20. BIO、NIO和AIO的区别(简明版)

热门文章

  1. Kotlin学习笔记
  2. vue 多选框 checkbox 父到子传值
  3. python3 schedule定时任务
  4. 执行 systemctl start firewalld 命令后出现Failed to start firewalld.service: Unit is masked
  5. AcWing 196. 质数距离(筛法+离散化)打卡
  6. AutoCAD二次开发-使用ObjectARX向导创建应用程序(HelloWorld例子)
  7. POJ3630-Phone List-Trie字典树模板题
  8. [Flink原理介绍第四篇】:Flink的Checkpoint和Savepoint介绍
  9. 6.2_springboot2.x分布式整合Dubbo
  10. pcA降维 SVD