时钟显示

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.scene.control.TextField;

public class DisplayClock extends Application{
@Override
public void start(Stage primaryStage) {
	ClockPane clock = new ClockPane();
	String timeString = clock.getHour()+":"+clock.getMinute()+":"+clock.getSecond();
	Label lblCurrentTime = new Label(timeString);
	TextField tf1 = new TextField("8");
	TextField tf2 = new TextField("0");
	TextField tf3 = new TextField("0");
	Label a1 = new Label(":");
	Label a2 = new Label(":");
	HBox hbox = new HBox();
	hbox.getChildren().addAll(tf1,a1,tf2,a2,tf3);
	BorderPane pane = new BorderPane();
	pane.setCenter(clock);
	pane.setBottom(hbox);
	tf1.setOnAction(e->{
		clock.setHour(Integer.parseInt(tf1.getText()));
		clock.setMinute(Integer.parseInt(tf2.getText()));
		clock.setSecond(Integer.parseInt(tf3.getText()));
		clock.paintClock();
	});
	tf2.setOnAction(e->{
		clock.setHour(Integer.parseInt(tf1.getText()));
		clock.setMinute(Integer.parseInt(tf2.getText()));
		clock.setSecond(Integer.parseInt(tf3.getText()));
		clock.paintClock();
	});
	tf3.setOnAction(e->{
		clock.setHour(Integer.parseInt(tf1.getText()));
		clock.setMinute(Integer.parseInt(tf2.getText()));
		clock.setSecond(Integer.parseInt(tf3.getText()));
		clock.paintClock();
	});
	pane.setTop(lblCurrentTime);
	BorderPane.setAlignment(lblCurrentTime, Pos.TOP_CENTER);
	Scene scene = new Scene(pane);
	primaryStage.setTitle("DisplayClock");
	primaryStage.setScene(scene);
	primaryStage.show();
}
	public static void main(String[] args) {
		
		launch(args);
	}

}

import java.util.Calendar;
import java.util.GregorianCalendar;
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;
public class ClockPane extends Pane{
	private int hour;
	private int minute;
	private int second;
	private double w = 450,h = 450;
	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();
	}
	public void setCurrentTime() {
		Calendar calendar = new GregorianCalendar();
		this.hour = calendar.get(Calendar.HOUR_OF_DAY);
		this.minute = calendar.get(Calendar.MINUTE);
		this.second = calendar.get(Calendar.SECOND);
		paintClock();
	}
	protected 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(Color.WHITE);
		circle.setStroke(Color.BLACK);
		Text t1 = new Text(centerX-5,centerY-clockRadius + 12,"12");
		Text t2 = new Text(centerX-clockRadius+3,centerY+5,"9");
		Text t3 = new Text(centerX+clockRadius-10,centerY+3,"3");
		Text t4 = 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 xMinute = 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,xMinute,minuteY);
		mLine.setStroke(Color.BLUE);
		//时针
		double hLength = clockRadius*0.5;
		double hourX = centerX+hLength*Math.sin((hour%12+minute/60.0)*(2*Math.PI/12));
		double hourY = centerY-hLength*Math.cos((hour%12+minute/60.0)*(2*Math.PI/12));
		Line hLine = new Line(centerX,centerY,hourX,hourY);
		hLine.setStroke(Color.GREEN);
		
		getChildren().clear();
		getChildren().addAll(circle,t1,t2,t3,t4,sLine,mLine,hLine);
	}
}

你可能感兴趣的:(时钟显示)