Javafx 简明教程
JavaFX - Slider
Slider 是一个 UI 控件,由一个轨道和一个滑块以及其他可选元素(如刻度线和刻度标签)组成。它允许用户通过沿着轨道拖动滑块从连续或离散的值范围内选择一个值。滑块的常见应用可以在音频或视频播放器、游戏、亮度控制器等中看到。下图显示了两个滑块,一个音量控制器,另一个亮度控制器。
Slider is a UI control consisting of a track and a thumb along with other optional elements like tick marks and tick labels. It allows the user to select a value from a continuous or discrete range of values by dragging the thumb along a track. The common applications of a slider can be seen in audio or video players, games, brightness controllers and so on. The figure below shows two sliders, one is a volume controller and the other one is a brightness controller.
Slider in JavaFX
在 JavaFX 中,滑块由名为 Slider 的类表示。此类属于包 javafx.scene.control 。通过实例化此类,我们可以在 JavaFX 中创建滑块。Slider 类的构造函数如下所列 −
In JavaFX, the slider is represented by a class named Slider. This class belongs to the package javafx.scene.control. By instantiating this class, we can create a slider in JavaFX. Constructors of the Slider class are listed below −
-
Slider() − It is the default constructor.
-
Slider(double minVal, double maxVal, double currentVal) − It creates a new slider with the specified initial, minimum and maximum values.
Steps to create a Slider in JavaFX
要在 JavaFX 中创建滑块,请遵循以下步骤。
To create a Slider in JavaFX, follow the steps given below.
Step 1: Instantiate the Slider class
在 start() 方法中实例化 Slider 类。此操作将在 JavaFX 应用程序中创建一个滑块控件。
Instantiate the Slider class inside the start() method. This action will create a slider control in JavaFX application.
// creating a slider using default constructor
Slider slide = new Slider();
Step 2: Set the minimum and maximum values of Slider
每个滑块都有最小值和最大值。要设置最小值,使用名为 setMin() 的内建方法,它接受一个 double 值作为参数。类似地,最大值使用 setMax() 方法设置,该方法也需要 double 作为参数值。
Every slider comes with minimum and maximum values. To set the minimum value, a built-in method named setMin() is used which accepts a double value as an argument. Similarly, the maximum value is set using the setMax() method, which also requires double as a parameter value.
// setting the minimum and maximum values
slide.setMin(0);
slide.setMax(100);
Step 3: Set the Orientation of the Slider
要设置滑块的方向,我们使用名为 setOrientation() 的方法。它接受垂直或水平枚举值作为参数。
To set the orientation of the Slider, we use the method named setOrientation(). It accepts enum values VERTICAL or HORIZONTAL as an argument.
// setting the orientation
slide.setOrientation(Orientation.VERTICAL);
Step 4: Optional Properties of Slider
我们还可以设置 Slider 的各种属性,例如主刻度和次刻度计数、刻度标记和标签。为此,请使用以下代码:
We can also set the various properties of the Slider such as major and minor tick counts, tick marks and labels. To do so, use the following code −
// major and minor tick marks
slide.setMajorTickUnit(10);
slide.setMinorTickCount(9);
slide.setShowTickMarks(true);
slide.setShowTickLabels(true);
Step 5: Launch the Application
创建 Slider 并设置其属性后,请按照以下给定的步骤正确启动应用程序:
Once the Slider is created and its properties are set, follow the given steps below to launch the application properly −
-
Firstly, create a VBox that holds the slider.
-
Next, instantiate the class named Scene by passing the VBox object as a parameter value to its constructor along with the dimensions of the application screen.
.
-
Then, set the title of the stage using the setTitle() method of the Stage class.
-
Now, a Scene object is added to the stage using the setScene() method of the class named Stage.
-
Display the contents of the scene using the method named show().
-
Lastly, the application is launched with the help of the launch() method.
Example
在以下示例中,我们将在 JavaFX 应用程序中创建一个滑动条。将此代码保存在名为 SliderJavafx.java 的文件中。
In the following example, we are going to create a slider in JavaFX application. Save this code in a file with the name SliderJavafx.java.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
public class SliderJavafx extends Application {
@Override
public void start(Stage stage) {
// creating a slider using default constructor
Slider slide = new Slider();
// setting the minimum and maximum values
slide.setMin(0);
slide.setMax(100);
// setting the orientation
slide.setOrientation(Orientation.VERTICAL);
// major and minor tick marks
slide.setMajorTickUnit(10);
slide.setMinorTickCount(9);
slide.setShowTickMarks(true);
slide.setShowTickLabels(true);
// creating a label to display the slider value
Label valueLabel = new Label("Value: " + slide.getValue());
// creating a listener to update the label when the slider value changes
slide.valueProperty().addListener((observable, oldValue, newValue) -> {
valueLabel.setText("Value: " + newValue.intValue());
});
// creating a VBox to hold the slider and the label
VBox root = new VBox();
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(10));
root.setSpacing(10);
root.getChildren().addAll(slide, valueLabel);
// create a scene and stage
Scene scene = new Scene(root, 400, 300);
stage.setTitle("Slider in JavaFX");
stage.setScene(scene);
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 SliderJavafx.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls SliderJavafx
当我们执行上述代码时,它将生成以下输出。
When we execute the above code, it will generate the following output.
Creating a Slider in JavaFX using its parameterized constructor
我们都知道有两种方法可以在 JavaFX 中创建滑动条:一种是借助 Slider 类的默认构造函数,另一种是使用其参数化构造函数。在下一个示例中,我们将使用 Slider 类的参数化构造函数在 JavaFX 中创建一个滑动条。将此代码保存在名为 JavafxSlider.java 的文件中。
We all know that there are two ways to create a slider in JavaFX: one with the help of default constructor of the Slider class, while the other uses its parameterized constructor. In the next example, we are going to use the parameterized constructor of the Slider class to create a slider in JavaFX. Save this code in a file with the name JavafxSlider.java.
Example
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
public class JavafxSlider extends Application {
@Override
public void start(Stage stage) {
// creating a Slider with a range of 0 to 100
Slider slide = new Slider(0, 100, 50);
// setting the orientation to horizontal
slide.setOrientation(Orientation.HORIZONTAL);
// setting the major tick unit to 10
slide.setMajorTickUnit(10);
// to show the tick marks and labels
slide.setShowTickMarks(true);
slide.setShowTickLabels(true);
// Label to display the value of the Slider
Label label = new Label("Value: " + slide.getValue());
// listener to show the value of Slider
slide.valueProperty().addListener((observable, oldValue, newValue) -> {
// Update the Label with the new value
label.setText("Value: " + newValue);
});
// creating a VBox
VBox root = new VBox(10);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(10));
root.getChildren().addAll(slide, label);
// creating a Scene and Stage
Scene scene = new Scene(root, 400, 300);
stage.setTitle("Slider in JavaFX");
stage.setScene(scene);
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 JavafxSlider.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxSlider
在执行上述代码时,它将生成以下输出。
On executing the above code, it will generate the following output.