JavaFx是開發Java GUI程序的新框架。JavaFX應用可以無縫地在桌面或web瀏覽器中運行。具有內建的2D、3D動畫支持,以及視頻和音頻的回放功能,可以作為一個應用獨立運行或者在瀏覽器中運行。
ClockPane類:
import javafx.scene.shape.Circle; import javafx.scene.shape.Line; import javafx.scene.text.Text; import javafx.scene.control.Label; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import java.util.Calendar; import java.util.GregorianCalendar; public 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 int getMinute() { return minute; } //返回秒鍾 public int getSecond() { return second; } //設置一個新的小時 public void setHour(int hour) { this.hour = hour; paintClock(); } //設置一個新的分鍾 public void setMinute(int minute) { this.minute = minute; paintClock(); } //設置一個新的秒鍾 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(); } public void paintClock() { //初始化時鍾參數 double clockRadius = Math.min(w, h) * 0.8 * 0.5; double centerX = w / 2; double centerY = h / 2; //顯示當前時間,並設置時間顯示的位置 String timeString = hour + ":" + minute + ":" + second; Label labelCurrentTime = new Label(timeString); labelCurrentTime.setLayoutX(centerX - 25); labelCurrentTime.setLayoutY(centerY + clockRadius + 5); //畫圓 Circle circle = new Circle(centerX, centerY, clockRadius); //circle.setFill(Color.WHITE); //circle.setStroke(Color.BLACK); circle.setStyle("-fx-stroke: black; -fx-fill: white"); 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 + 5, "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.0)); double secondY = centerY - sLength * Math.cos(second * (2 * Math.PI / 60.0)); 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.0); double minuteY = centerY - mLength * Math.cos(minute * 2 * Math.PI / 60.0); Line mLine = new Line(centerX, centerY, minuteX, 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, labelCurrentTime); } }
顯示時鍾程序:
import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; import javafx.util.Duration; public class ClockAniamtion extends Application{ @Override public void start(Stage primaryStage) throws Exception { ClockPane clock = new ClockPane(); //將clock放在pane里 BorderPane pane = new BorderPane(); pane.setCenter(clock); //創建一個handler EventHandler<ActionEvent> eventHandler = e -> { clock.setCurrentTime(); }; Timeline animation = new Timeline( new KeyFrame(Duration.millis(1000), eventHandler)); animation.setCycleCount(Timeline.INDEFINITE); animation.play(); Scene scene= new Scene(pane, 250, 250); primaryStage.setTitle("時鍾"); primaryStage.setScene(scene); primaryStage.show(); //讓clock跟隨窗口的變化而變化 pane.widthProperty().addListener(ov -> clock.setW(pane.getWidth())); pane.heightProperty().addListener(ov -> clock.setH(pane.getHeight())); } public static void main(String[] args) { Application.launch(args); } }
