如何在 JavaFX 中创建微调器?


微调器是一种 UI 元素,它显示一个数字,并带有向上和向下箭头。可以使用这些箭头增大或减小微调器的值。可通过实例化 javafx.scene.control.Spinner 类来创建一个微调器。

实例

以下示例演示如何创建 **微调器**。

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Spinner;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class SpinnerExample extends Application {
   public void start(Stage stage) {
      //Setting the label
      Label label = new Label("Select Date:");
      Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
      label.setFont(font);
      //Creating a spinner
      Spinner month = new Spinner(1, 12, 4);
      //Setting the spinner editable
      month.setEditable(true);
      //Setting the size
      month.setPrefSize(75, 25);
      Label label1 = new Label("Month: ");
      //Creating a spinner
      Spinner day = new Spinner(1, 31, 4);
      //Setting the spinner editable
      day.setEditable(true);
      //Setting the size
      day.setPrefSize(75, 25);
      Label label2 = new Label("Day: ");
      //Creating a spinner
      Spinner year = new Spinner(1947, 2999, 2009);
      //Setting the spinner editable
      year.setEditable(true);
      //Setting the size
      year.setPrefSize(75, 25);
      Label label3 = new Label("Year: ");
      HBox hbox = new HBox(5);
      hbox.setPadding(new Insets(10, 10, 10, 25));
      hbox.getChildren().addAll(label1, month, label2, day, label3, year);
      //Creating a vbox to hold the pagination
      VBox vbox = new VBox();
      vbox.setSpacing(5);
      vbox.setPadding(new Insets(10, 10, 10, 25));
      vbox.getChildren().addAll(label, hbox);
      //Setting the stage
      Group root = new Group(vbox);
      Scene scene = new Scene(root, 595, 200, Color.BEIGE);
      stage.setTitle("Spinner");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

输出

更新于:2020-05-19

2K+ 浏览量

开启你的 职业生涯

完成课程后获得认证

开始
广告