Javafx 简明教程
JavaFX - Spinner
Spinner 是一个 UI 控件,允许用户从预定义范围或有序序列中选择一个值。它可以是可编辑的或不可编辑的。如果它可编辑,则用户可以键入值,否则不能键入。它还提供向上和向下箭头,以便用户可以浏览序列值。下图展示了一个微调器 −
A Spinner is a UI control that allows the user to select a value from a predefined range or a ordered sequence. It can be either editable or non-editable. If it is editable, the user can type in a value otherwise not. It also provides up and down arrows so that a user can step through the values of sequence. The figure below illustrates a spinner −
Creating a Spinner in JavaFX
在 JavaFX 中,通过实例化一个名为 Spinner 的类来创建微调器。此类属于包 javafx.scene.control 。列出 Spinner 类的部分广泛使用的构造函数如下 −
In JavaFX, the spinner is created by instantiating a class named Spinner. This class belongs to the package javafx.scene.control. Some of the widely used constructors of the Spinner class are listed below −
-
Spinner() − It is used to create an empty spinner.
-
Spinner(double minVal, double maxVal, double initialVal) − It creates a new spinner with the specified minimum, maximum and initial values.
-
Spinner(double minVal, double maxVal, double initialVal, double valToStepBy) − It is used to construct a new spinner with the specified minimum, maximum and initial values along with the increment amount.
Example
以下 JavaFX 程序演示如何在 JavaFX 应用程序中创建纺纱器。将此代码保存在名为 JavafxSpinner.java 的文件中。
The following JavaFX program demonstrates how to create a spinner in JavaFX application. Save this code in a file with the name JavafxSpinner.java.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Spinner;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.geometry.Pos;
public class JavafxSpinner extends Application {
@Override
public void start(Stage stage) {
// creating a label
Label newlabel = new Label("Sample Spinner: ");
// creating spinner and setting min, max, initial value
Spinner newSpinner = new Spinner(0, 100, 25);
// vbox to hold spinner
VBox vbox = new VBox(newlabel, newSpinner);
vbox.setAlignment(Pos.CENTER);
// creating stage and scene
Scene scene = new Scene(vbox, 400, 300);
stage.setScene(scene);
stage.setTitle("Spinner in JavaFX");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
要从命令提示符编译并执行已保存的 Java 文件,请使用以下命令−
To compile and execute the saved Java file from the command prompt, use the following commands −
javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxSpinner.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxSpinner
当我们执行上述代码时,它将生成以下输出。
When we execute the above code, it will generate the following output.
Setting the Size of Spinner
要设置纺纱器的大小,可以使用 setPrefSize() 方法。这是一个内置方法,它接受高度和宽度作为参数。
To set the size of a spinner, we can use setPrefSize() method. It is a built-in method which accepts heigth and width as a parameter.
Example
在以下示例中,我们将在 JavaFX 应用程序中创建一个指定大小的 Spinner。将此代码保存在名为 DemoSpinner.java 的文件中。
In the following example, we are going to create a Spinner of the specified size in JavaFX application. Save this code in a file with the name DemoSpinner.java.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Spinner;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.geometry.Pos;
public class DemoSpinner extends Application {
@Override
public void start(Stage stage) {
// creating labels for spinner
Label newlabel = new Label("Enter Date of Birth: ");
Label setYear = new Label("Year: ");
Label setMonth = new Label("Month: ");
Label setDay = new Label("Day: ");
// creating spinners and setting sizes
Spinner year = new Spinner(1998, 2020, 2000);
year.setPrefSize(65, 25);
Spinner month = new Spinner(1, 12, 1);
month.setPrefSize(60, 25);
Spinner day = new Spinner(1, 31, 1);
day.setPrefSize(60, 25);
// HBox to hold labels and spinners
HBox box1 = new HBox();
box1.setPadding(new Insets(15, 12, 15, 12));
box1.setSpacing(10);
box1.getChildren().addAll(setYear, year, setMonth, month, setDay, day);
// VBox to hold HBox and Label
VBox box2 = new VBox();
box2.setAlignment(Pos.CENTER);
box2.setPadding(new Insets(15, 12, 15, 12));
box2.setSpacing(10);
box2.getChildren().addAll(newlabel, box1);
// creating scene and stage
Scene scene = new Scene(box2, 400, 400);
stage.setScene(scene);
stage.setTitle("Spinner in JavaFX");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
通过使用以下命令从命令提示符编译并执行保存的 Java 文件 −
Compile and execute the saved Java file from the command prompt by using the following commands −
javac --module-path %PATH_TO_FX% --add-modules javafx.controls DemoSpinner.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls DemoSpinner
在执行上述代码时,它将生成以下输出。
On executing the above code, it will generate the following output.