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);
}
}
运行效果图