1.首先创建一个时钟类,用于编写时钟的各种特有属性

package javaclock;

/**
*
* @author admin
*/
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;

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;
import javafx.scene.control.Button;
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) {
// Button btn = new Button("调整时间");
//btn.setCurrentTime1();
this.hour = hour;
this.minute = minute;
this.second = second;
paintClock();//调用方法
}

//set与get方法

public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
paintClock();
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = minute;
paintClock();

}
public int getSecond() {
return second;
}
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 setCurrentTime1(int hour,int minute,int second){
this.hour = hour;
this.minute = minute;
this.second = second;
paintClock();
}
//方法setCurrentTime()设置获取当前时间
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();
}

//创建paintClock()显示时间
public void paintClock() {
double clockRadius = Math.min(w, h)*0.8*0.5;//时钟半径
double centerX = w/2;//圆心坐标
double centerY = h/2;

//创建时钟圆形
Circle circle = new Circle(centerX,centerY,clockRadius);//创建圆
circle.setFill(Color.WHITE);//圆形背景色为白色
circle.setStroke(Color.BLACK);//圆形边缘色为黑色

//创建四个文本对象用于将12,9,6,3填入圆形中,注意不要压在圆形上面,需要计算坐标
Text text1 = new Text(centerX-5,centerY-clockRadius+12,"12");
Text text2 = new Text(centerX-clockRadius+3,centerY+5,"9");
Text text3 = new Text(centerX+clockRadius-10,centerY+3,"3");
Text text4 = new Text(centerX-3,centerY+clockRadius-3,"6");

//分别绘制时针,分针,秒针
//秒针
double secLength = clockRadius*0.8;//秒针长度
double secondX = centerX + secLength*Math.sin(second*(2*Math.PI/60));//因为一分钟有60秒,故求出当前秒针的角度,利用三角函数公式即可计算出秒针的端点x坐标
double secondY = centerY - secLength*Math.cos(second*(2*Math.PI/60));//同理求出秒针的端点Y坐标
Line secline = new Line(centerX,centerY,secondX,secondY);//创建线段对象实现秒针
secline.setStroke(Color.RED);//将秒针定义为红色
//分针
double minLength = clockRadius*0.65;//分针长度
double minuteX = centerX + minLength*Math.sin((minute )*(2*Math.PI/60));//因为一小时有六十分钟,利用分针的时间加上秒针的时间,利用三角函数对应的角度,即可计算出分针的端点x坐标
double minuteY = centerY - minLength*Math.cos((minute )*(2*Math.PI/60));//同理求出分针的端点Y坐标
Line minline = new Line(centerX,centerY,minuteX,minuteY);//创建线段对象实现秒针
minline.setStroke(Color.GREEN);//将秒针定义为绿色
//时针
double houLength = clockRadius*0.5;//时针长度
double hourX = centerX + houLength*Math.sin((hour%12 + minute/60.0 )*(2*Math.PI/12));//因为一天有十二小时,一小时有六十分钟,利用小时的时间加上分针的时间再加上秒针的时间,利用三角函数对应的角度,即可计算出分针的端点x坐标
double hourY = centerY - houLength*Math.cos((hour%12 + minute/60.0 )*(2*Math.PI/12));//同理求出时针的端点Y坐标
Line houline = new Line(centerX,centerY,hourX,hourY);//创建线段对象实现时针
houline.setStroke(Color.BLUE);//将秒针定义为蓝色

getChildren().clear();//每调用执行一次paintClock()方法就会清空面板
getChildren().addAll(circle,text1,text2,text3,text4,secline,minline,houline);//将几个控件添加到面板中
}

}

2、然后编写一个测试类,用于通过多线程创建呈现时钟的动画效果

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaclock;
import java.util.*;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.layout.BorderPane;
import javafx.animation.Timeline;
import javafx.scene.control.Label;
import javafx.animation.KeyFrame;
import javafx.geometry.Pos;
import javafx.util.Duration;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
/**
*
* @author admin
*/
public class JavaClock extends Application {
//JavaClock jc = new JavaClock();
ClockPane clock = new ClockPane();//
BorderPane borderPane = new BorderPane();//
@Override
public void start(Stage primaryStage) throws Exception {
// TODO 自动生成的方法存根

//绑定事件源
EventHandler<ActionEvent> eventHandler = e ->{
clock.setCurrentTime();

String timeString = clock.getHour()+":"+clock.getMinute()+":"+clock.getSecond();
Label labelCurrentTime = new Label(timeString);
borderPane.setCenter(clock);
borderPane.setBottom(labelCurrentTime);
BorderPane.setAlignment(labelCurrentTime, Pos.TOP_CENTER);
};

Timeline animation = new Timeline(new KeyFrame(Duration.millis(1000),eventHandler));//设定时钟动画每1秒变一次,关键帧时间间隔
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();

//Scene
Scene scene = new Scene(borderPane,250,250);
primaryStage.setTitle("JavaClock");
primaryStage.setScene(scene);
primaryStage.show();//展示场景

borderPane.widthProperty().addListener(o->
clock.setW(borderPane.getWidth()));//保持时间面板与场景同步
borderPane.heightProperty().addListener(o->
clock.setH(borderPane.getHeight()));
//设置一个按钮,用于调整时间

}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

}

利用javafx编写一个时钟制作程序的更多相关文章

  1. Java基础-接口中国特色社会主义的体制中有这样的现象:地方省政府要坚持党的领导和按 照国务院的指示进行安全生产。请编写一个java应用程序描述上述的体制现象。 要求如下: (1)该应用程序中有一个“党中央”接口:CentralPartyCommittee,该接口中 有个“坚持党的领导”方法:void partyLeader() (2)该应用程序中有一个“国务院”抽象类:StateCouncil,

    36.中国特色社会主义的体制中有这样的现象:地方省政府要坚持党的领导和按 照国务院的指示进行安全生产.请编写一个java应用程序描述上述的体制现象. 要求如下: (1)该应用程序中有一个“党中央”接口 ...

  2. Java基础-继承-编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个 类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功 能。

    #29.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight.小车类Car是Vehicle的子类,其中包含的属性有载人数 loader.卡车类T ...

  3. java基础,继承类题目:编写一个Java应用程序,该程序包括3个类:Monkey类、People类和主类 E

    21.编写一个Java应用程序,该程序包括3个类:Monkey类.People类和主类 E.要求: (1) Monkey类中有个构造方法:Monkey (String s),并且有个public vo ...

  4. C#编写一个控制台应用程序,输入正方形边长或者半径,计算其周长和面积并输出

    编写一个控制台应用程序,输入正方形边长或者半径,计算其周长和面积并输出 (1) 编写两个接口,接口 IShape 包含三个方法:initialize, getPerimeter, getArea.分别 ...

  5. C#编写一个控制台应用程序,可根据输入的月份判断所在季节

    编写一个控制台应用程序,可根据输入的月份判断所在季节 代码: using System; using System.Collections.Generic; using System.Linq; us ...

  6. C#编写一个控制台应用程序,输入三角形或者长方形边长,计算其周长和面积并输出

    编写一个控制台应用程序,输入三角形或者长方形边长,计算其周长和面积并输出. 代码: using System; using System.Collections.Generic; using Syst ...

  7. SpringMVC入门--编写一个SpringMVC小程序

    一.SpringMVC的优势 Spring 为展现层提供的基于 MVC 设计理念的优秀的Web 框架,是目前最主流的 MVC 框架之一.Spring3.0 后全面超越 Struts2,成为最优秀的 M ...

  8. 编写一个JAVA小程序取得IP地址

    在TCP/IP 互联网时,经常会需要查询自己主机的IP地址和www服务器的IP地址.虽然,我们可以使用IPCONFIG 和PING 进行IP地址查询,但是如果在应用程序或APPLET中使用此命令会破坏 ...

  9. 编写一个小Servlet程序

    1.编写一个java类,此类继承HttpServlet 创建完工程(见上一篇随笔:使用eclipse创建在myeclipse中运行的web工程),在src中新建一个包,包名字叫servlet:再新建一 ...

随机推荐

  1. Linux命令查看文件内容

    cat:一次性顺序显示文件所有内容和 cat filename tac:一次性倒序显示文件所有内容和 tac filename head:显示文件开头的若干行内容 head -n filename t ...

  2. 在DataWorks中实现指定UDF只能被指定账户访问

    背景 之前写过一篇文章是关于“DataWorks和MaxCompute内部权限体系的区别”有兴趣的朋友可以点击阅读查看详情.但是还是有些同学会问,我如何在DataWorks中实现我的具体某个Resou ...

  3. 集合划分——cf1028D思维题

    非常思维的一道题目,题意很长 给定s1,s2两个集合,s1维护最大值,s2维护最小值,s1的所有元素要比s2小 操作1:往两个集合里的任意一个添加x 操作2:把x从所在的集合里删掉:要求被删的x必须是 ...

  4. AnsiString, String, char,char

    AnsiString 是一个类,String 是一个结构,char* 是一个指针 .String是Pascal的类型原型,因为C++中没有字符串数据类型的,因此使用char*来存储,char*必须是以 ...

  5. 深入浅出 Java Concurrency (28): 线程池 part 1 简介[转]

    从这一节开始正式进入线程池的部分.其实整个体系已经拖了很长的时间,因此后面的章节会加快速度,甚至只是一个半成品或者简单化,以后有时间的慢慢补充.完善. 其实线程池是并发包里面很重要的一部分,在实际情况 ...

  6. Template-Thymeleaf:Thymeleaf

    ylbtech-Template-Thymeleaf:Thymeleaf 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 0. https://www.thyme ...

  7. 《DSP using MATLAB》Problem 8.4

    今天是六一儿童节,陪伴不了家人,心里思念着他们,看着地里金黄的麦子,远处的山,高高的天 代码: %% ------------------------------------------------- ...

  8. C# 读写 Photoshop PSD文件 操作类

    分析了PSD的文件....才发现PSD的RGB色彩保存也是 先红 蓝 绿 这样保存的 ....麻烦的.. 另外8BIM好象没什么用..可以直接跳过..直接获取最后的图形信息就可以了.. 我只对一些PS ...

  9. Ubuntu 16.04 安装STS

    先将STS下载下来,网址是 https://spring.io/tools/sts/all ,然后将STS压缩包移动或者copy到想要放置的位置,比如, sudo cp spring-tool-sui ...

  10. vbox搭建centos常用技巧

    初始化 :vbox系统网络连接方式用桥接 :ping不通,请修改/etc/sysconfig/network-scripts/ifcfg-eth0 ONBOOT=no 改成 ONBOOT=yes 然后 ...