如何在 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+ 瀏覽量

開啟你的 職業生涯

完成課程後獲得認證

開始
廣告