JavaFX —— DateTimePicker日期时间选择框

今天介绍一个开源的JavaFX组件DateTimePicker,因为在官方的UI组件库里对于时间的设置,只有一个DatePicker这个组件,我需要一个可以设置日期加时间的控件,所以在网上找到了一个前辈的开源组件,下面先看一下效果图

在这里插入图片描述

使用方法

1.大家可以去GitHub上的项目地址下载源代码,然后导入到我们自己的项目中,然后再Custom中就可以看到DateTimePicker了,就可以直接是用了。

在这里插入图片描述

2.大家也可以直接导入jar包,这个是我自己打包的。

链接: https://pan.baidu.com/s/1p57LXLEi15Re23WYS17FQg 提取码: yfmi 复制这段内容后打开百度网盘手机App,操作更方便哦

代码

FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import com.browniebytes.javafx.control.DateTimePicker?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.ergou.controller.DateTimePickerController">
   <children>
      <DateTimePicker fx:id="dateTimePicker" layoutX="54.0" layoutY="29.0" prefHeight="100.0" prefWidth="200.0" />
      <Button fx:id="button" layoutX="339.0" layoutY="29.0" mnemonicParsing="false" text="设置一个固定时间" />
      <Button fx:id="getbutton" layoutX="339.0" layoutY="79.0" mnemonicParsing="false" text="获取时间" />
      <Label fx:id="label" layoutX="169.0" layoutY="192.0" />
   </children>
</AnchorPane>

Controller

import com.browniebytes.javafx.control.DateTimePicker;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;

import java.net.URL;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ResourceBundle;

public class DateTimePickerController implements Initializable {
    @FXML
    private DateTimePicker dateTimePicker;
    @FXML
    private Button button;
    @FXML
    private Button getbutton;
    @FXML
    private Label label;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        button.setOnAction(event -> {
            String dateString = "2018-09-10 15:33:42";
            LocalDateTime localDateTime = LocalDateTime.parse(dateString, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            dateTimePicker.dateTimeProperty().set(localDateTime);//设置DateTimePicker要显示的时间
        });
        getbutton.setOnAction(event -> {
            label.setText(dateTimePicker.dateTimeProperty().get().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));//获取DateTimePicker里的时间
        });
    }
}

Java8中 Date和LocalDateTime的相互转换

Java8中 Date和LocalDate的相互转换

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章