JAVA fx 时钟

JAVA fx 时钟

虽然没那么完美,但是可以凑合

此时钟是静态的,不是动态的,他会获取电脑当前运行代码是的时间并且显示

package javaFxUI;

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.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import static javafx.scene.paint.Color.*;
import java.util.Calendar;
import java.util.GregorianCalendar;




public class clock extends Application {
    public void start(Stage primaryStage) {
//        create a pane and a label
        ClockPane clockPane = new ClockPane();

        String timestring = clockPane.getHour() + " : " + clockPane.getMinute() + " : " + clockPane.getSecond();
        Label labelCurrentTime = new Label(timestring);

        BorderPane pane = new BorderPane();
        pane.setCenter(clockPane);
        pane.setBottom(labelCurrentTime);
        BorderPane.setAlignment(labelCurrentTime, Pos.TOP_CENTER);

        Scene scene = new Scene(pane, 600,600);
        primaryStage.setTitle("clock");
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

class ClockPane extends Pane{
    private int hour , minute, second;
//    设置clockpane的宽度和高度
    private double width_clock = 600, height_clock = 600;

//    用当前时间构造默认时钟
    public ClockPane(){
        setCurrentTime();
    }

//用指定的时、分、秒构造时钟
    public ClockPane(int hour , int minute, int second){
        this.hour = hour;
        this.minute = minute;
        this.second = second;
        PaintClock();
    }
//    return hour
    public int getHour(){ return hour; }
//    set a new hour
    public void setHour(int hour){
        this.hour = hour;
        PaintClock();
    }
//    return minute
    public int  getMinute(){return minute;}
//    set a new minute
    public void setMinute(int minute){
        this.minute = minute;
        PaintClock();
    }
//  return second
    public int getSecond(){ return second; }
//    set a new second
    public void setSecond(int second){
        this.second = second;
        PaintClock();
    }
//    return clockpanes width
    public double getWidth_clock(){ return width_clock; }
//    set clockoane width
    public void setWidth_clock(double width_clock){
        this.width_clock = width_clock;
        PaintClock();
    }
//    return clockpane height
    public double getHeight_clock(){return height_clock;}
//    set clockpanes height
    public void setHeight_clock(double height_clock){
        this.height_clock = height_clock;
        PaintClock();
    }
//    set current time for the clock
    public void setCurrentTime(){
//        construct a caledar for the current dataand time
        Calendar calendar = new GregorianCalendar();

//        set currect hour minute second
        this.hour = calendar.get(Calendar.HOUR_OF_DAY);
        this.minute = calendar.get(Calendar.MINUTE);
        this.second = calendar.get(Calendar.SECOND);
        PaintClock();
    }

//    paint the clock
    protected void PaintClock(){
//        initialize clock parementers
        double clockRadius = Math.min(width_clock, height_clock) * 0.8 *0.5;
        double centerX = width_clock / 2;
        double centerY = height_clock / 2;

//        draw circle
        Circle circle = new Circle(centerX,centerY,clockRadius);
        circle.setFill(WHITE);
        circle.setStroke(DARKBLUE);
        Text text = new Text(centerX - 5 , centerY -clockRadius +12, "12");
        Text text1 = new Text(centerX - clockRadius + 3, centerY - 5,"9");
        Text text2 = new Text(centerX + clockRadius - 10, centerY + 3, "3");
        Text text3 = new Text(centerX - 3, centerY + clockRadius - 3, "6");

//        draw second hand
        double slength = clockRadius * 0.8;
        double secondX = centerX + slength * Math.sin(second *(2 * Math.PI / 60));
        double secondY = centerX - slength * Math.cos(second *(2 * Math.PI / 60));
        Line sline = new Line(centerX,centerY,secondX,secondY);
        sline.setStroke(RED);

//        draw minute hand
        double mlength = clockRadius * 0.65;
        double minuteX = centerX + mlength * Math.sin(minute *(2 * Math.PI / 60));
        double minuteY = centerX - mlength * Math.cos(minute *(2 * Math.PI / 60));
        Line mline = new Line(centerX,centerY,minuteX,minuteY);
        mline.setStroke(BLUE);


//                draw hour hand
        double hlength = clockRadius * 0.5;
        double hourX = centerX + hlength * Math.sin((hour % 12 + minute / 60.0) *(2 * Math.PI / 12));
        double hourY = centerX - hlength * Math.cos((hour % 12 + minute / 60.0)*(2 * Math.PI / 12));
        Line hline = new Line(centerX,centerY,hourX,hourY);
        hline.setStroke(BLACK);

        getChildren().clear();
        getChildren().addAll(circle,text,text1,text2,text3,sline,mline,hline);
    }
}

在这里插入图片描述
运行效果图

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值