java JavaFX 实时时间和日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42383857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:35:14  来源:igfitidea点击:

JavaFX Live Time and Date

javajavafxjavafx-8

提问by Bryan Reyes

I am building currently an application using JavaFx with an extra feature that displays the current date and time in the top corner of the scene. Since I am new to JavaFX, I don't know how to implement this one.

我目前正在使用 JavaFx 构建一个具有额外功能的应用程序,该功能在场景的顶角显示当前日期和时间。由于我是 JavaFX 的新手,我不知道如何实现这一点。

I tried to use an old code in swing but I got an IllegalStateException Error.

我尝试在 Swing 中使用旧代码,但出现 IllegalStateException 错误。

Here's my code.

这是我的代码。

MainMenuController.java

主菜单控制器.java

@FXML private Label time;

private int minute;
private int hour;
private int second;

@FXML
public void initialize() {

    Thread clock = new Thread() {
        public void run() {
            for (;;) {
                DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
                Calendar cal = Calendar.getInstance();

                second = cal.get(Calendar.SECOND);
                minute = cal.get(Calendar.MINUTE);
                hour = cal.get(Calendar.HOUR);
                //System.out.println(hour + ":" + (minute) + ":" + second);
                time.setText(hour + ":" + (minute) + ":" + second);

                try {
                    sleep(1000);
                } catch (InterruptedException ex) {
                     //...
                }
            }
        }
    };
    clock.start();
}

MainMenu.fxml

主菜单文件

 <children>
   <Label fx:id="time" textFill="WHITE">
     <font>
       <Font name="Segoe UI Black" size="27.0" />
     </font>
   </Label>
   <Label fx:id="date" textFill="WHITE">
     <font>
       <Font name="Segoe UI Semibold" size="19.0" />
     </font>
   </Label>
 </children>

Main.java

主程序

public class Main extends Application {

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

   @Override
   public void start(Stage primaryStage) throws Exception {
       Parent root = FXMLLoader.load(getClass().getResource("view/MainMenu.fxml"));
       primaryStage.setScene(new Scene(root,1366, 768));
       primaryStage.show();
   }
}

As you notice, I tested it printing the live time in the console. Yeah it worked, but the label is still static.

如您所见,我测试了它在控制台中打印实时时间。是的,它有效,但标签仍然是静态的。

回答by Shekhar Rai

I think you need FX UI Thread Platform.runLater(...)for that, but you can do something like this using Timelinein you controller class,

我认为你需要 FX UI Thread Platform.runLater(...),但是你可以Timeline在你的控制器类中使用类似的东西,

@FXML
public void initialize() {

    Timeline clock = new Timeline(new KeyFrame(Duration.ZERO, e -> {        
        LocalTime currentTime = LocalTime.now();
        time.setText(currentTime.getHour() + ":" + currentTime.getMinute() + ":" + currentTime.getSecond());
    }),
         new KeyFrame(Duration.seconds(1))
    );
    clock.setCycleCount(Animation.INDEFINITE);
    clock.play();
}

回答by WEGSY85

The @Shekhar Rai answer works well, but here is a shorter version who works pretty well too.

@Shekhar Rai 的答案效果很好,但这里有一个较短的版本,效果也很好。

@FXML
Label dateTime;

@Override
public void initialize(URL location, ResourceBundle resources) {
    initClock();
}

private void initClock() {

    Timeline clock = new Timeline(new KeyFrame(Duration.ZERO, e -> {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        dateTime.setText(LocalDateTime.now().format(formatter));
    }), new KeyFrame(Duration.seconds(1)));
    clock.setCycleCount(Animation.INDEFINITE);
    clock.play();
}

The main advantage is that you dont have to define every variables (seconds, minutes, ...)

主要优点是您不必定义每个变量(秒,分钟,...)

回答by Samer

i have a simple method it may helps you

我有一个简单的方法可以帮助你

private JFXDatePicker txtDateVenteAV;
txtDateVenteAV.setValue(LocalDate.now());