用JavaFX做一个静态时钟,使用标签显示小时、分钟和秒钟
实现代码:
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
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.stage.Stage;
import java.util.Calendar;
import java.util.GregorianCalendar;
/**
* @author xucc
* @date 2020/5/13 13:49
*/
public class DisplayClock extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
//创建一个时钟和面板
ClockPane clockPane = new ClockPane();
String timeString = clockPane.getHour() + ":" + clockPane.getMinute() +
":" + clockPane.getSecond();
Label labelTime = new Label(timeString);
//将时钟和面板放入BorderPane
BorderPane borderPane = new BorderPane();
borderPane.setCenter(clockPane);
borderPane.setBottom(labelTime);
BorderPane.setAlignment(labelTime, Pos.TOP_CENTER);
Scene scene = new Scene(borderPane, 250, 250);
primaryStage.setScene(scene);
primaryStage.setTitle("时钟");
primaryStage.show();
}
}
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 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();
}
private void setCurrentTime() {
//创建一个日期类
GregorianCalendar calendar = new GregorianCalendar();
//获取当前日期
this.hour = calendar.get(Calendar.HOUR_OF_DAY);
this.minute = calendar.get(Calendar.MINUTE);
this.second = calendar.get(Calendar.SECOND);
paintClock();
}
private 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(null);
circle.setStroke(Color.BLACK);
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 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.RED);
//创建分针
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.GREEN);
//创建时针
double hLength = clockRadius * 0.5;
double hourX = centerX + hLength * Math.sin((hour % 12 + minute /60) * (2 * Math.PI / 12));
double hourY = centerY - hLength * Math.cos((hour % 12 + minute / 60) * (2 * Math.PI / 12));
Line hline = new Line(centerX, centerY, hourX, hourY);
hline.setStroke(Color.YELLOW);
getChildren().clear();//每次调用paintClock()都要清楚以前的节点
getChildren().addAll(circle,text1,text2,text3,text4,sline,mline,hline);
}
}
输出结果: