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的相互轉換

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