JavaFX 在界面上显示实时时间
怎样在javaFX界面是上显示实时时间?
以下是我将时间显示在标签(Label)上的案例
首先,创建类继承Applcation并实现start方法
public class 类名 extends Application{ @Override public void start(Stage primaryStage) throws Exception { }
在start方法内完成
//设置实时时间 Label time = new Label(); //设置标签 time.setFont(new Font(20)); //设置标签字体 DateFormat currentTime = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss"); //设置时间格式 EventHandler<ActionEvent> eventHandler = e->{ time.setText(currentTime.format(new Date())); System.out.println(currentTime.format(new Date())); }; Timeline animation = new Timeline(new KeyFrame(Duration.millis(1000), (javafx.event.EventHandler<ActionEvent>) eventHandler)); //一秒刷新一次 animation.setCycleCount(Timeline.INDEFINITE);
将标签添加到面板上
pane.getChildren.add(time);