Java——显示一个模拟时钟, 使用标签显示小时、分钟和秒钟

用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);
    }
}

输出结果:
在这里插入图片描述

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值