使用JavaFX制作一个简单的日历
发布时间:2020-11-16 14:30:14
来源:亿速云
阅读:74
作者:Leah
这篇文章给大家介绍使用JavaFX制作一个简单的日历,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
具体内容如下
1.先看效果:
2.代码:
1)ClockEdge.java类
这个类(Pane)主要是用来设置时钟边缘(为了美观对直了半天,其实想想也没必要~。~)
package com.javaBasic.javaFX;
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;
/**
* @version 1.00 2016-10-15
* @author Administrator
* 功能:显示时钟边缘
*/
public class ClockEdge extends Pane{
private double w , h;
public ClockEdge(){
this(200, 200);
paint();
}
public ClockEdge(double w, double h){
this.w = w;
this.h = h;
paint();
}
public double getW() {
return w;
}
public void setW(double w) {
this.w = w;
paint();
}
public double getH() {
return h;
}
public void setH(double h) {
this.h = h;
paint();
}
public void paint(){
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);
//假设一个字符宽7.6个像素,高8.8个像素
Text text1 = new Text(centerX + clockRadius * Math.sin(1 * Math.PI / 6) - 7.6 + 1.7 * (1 + Math.cos(2 * Math.PI / 6)) - 2.2 * Math.sin(2 * Math.PI / 6),
centerY - clockRadius * Math.cos(1 * Math.PI / 6) + 8.8 + 1.7 * Math.sin(2 * Math.PI / 6) - 2.2 * (1 - Math.cos(2 * Math.PI / 6)), "1");
Text text2 = new Text(centerX + clockRadius * Math.sin(2 * Math.PI / 6) - 7.6 + 1.7 * (1 + Math.cos(4 * Math.PI / 6)) - 2.2 * Math.sin(4 * Math.PI / 6),
centerY - clockRadius * Math.cos(2 * Math.PI / 6) + 8.8 + 1.7 * Math.sin(4 * Math.PI / 6) - 2.2 * (1 - Math.cos(4 * Math.PI / 6)), "2");
Text text3 = new Text(centerX + clockRadius * Math.sin(3 * Math.PI / 6) - 7.6 + 1.7 * (1 + Math.cos(6 * Math.PI / 6)) - 2.2 * Math.sin(6 * Math.PI / 6),
centerY - clockRadius * Math.cos(3 * Math.PI / 6) + 8.8 + 1.7 * Math.sin(6 * Math.PI / 6) - 2.2 * (1 - Math.cos(6 * Math.PI / 6)), "3");
Text text4 = new Text(centerX + clockRadius * Math.sin(4 * Math.PI / 6) - 7.6 + 1.7 * (1 + Math.cos(8 * Math.PI / 6)) + 2.2 * Math.sin(8 * Math.PI / 6),
centerY - clockRadius