如何使用 JavaFX 创建日期选择器?
通常,日期选择器会显示一个日历,你可以用来浏览不同的月份并选择一个日期。选定的日期会被格式化并输入到系统中。
在 JavaFX 中,javafx.scene.control.DatePicker 类表示日期选择器。它包含一个文本(日期)文件和一个日期选择器。你可以手动输入日期,也可以使用日期选择器选择日期。
要在你的应用程序中创建日期选择器,你需要实例化此类。你可以通过调用getValue() 方法从日期选择器中获取选定的日期。
示例
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.DatePicker;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class DatePickerExample extends Application {
public void start(Stage stage) {
//Creating label
Text dobLabel = new Text("Date Of Birth:");
Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
dobLabel.setFont(font);
//Creating a date picker
DatePicker datePicker = new DatePicker();
//Creating HBox
HBox box = new HBox(5);
box.setPadding(new Insets(15, 50, 50, 50));
box.getChildren().addAll(dobLabel, datePicker);
//Setting the stage
Scene scene = new Scene(box, 595, 260, Color.BEIGE);
stage.setTitle("Date Picker Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP