JavaFX Slider

04/14 10:48
阅读数 95

The JavaFX Slider control provides a way for the user to select a value within a given interval by sliding a handle to the desired point representing the desired value. The JavaFX Slider is represented by the JavaFX class javafx.scene.control.Slider. Here is a screenshot of how a JavaFX Slider looks:

JavaFX Slider Example

Here is a full JavaFX Slider code example:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class SliderExample extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX App");

        Slider slider = new Slider(0, 100, 0);

        VBox vBox = new VBox(slider);
        Scene scene = new Scene(vBox, 960, 600);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

Create a Slider

To use a JavaFX Slider you must first create an instance of the Slider class. Here is an example of creating a JavaFX Slider instance:

Slider slider = new Slider(0, 100, 0);

The Slider constructor used above takes three parameters: The min value, the max value and the initial value. The min value is the value sliding the handle all the way to the left represents. This is the beginning of the interval the user can select a value in. The max value is the value sliding the handle all the way to the right represents. This the end of the interval the user can select a value in. The initial value is the value that the handle should be located at, when presented to the user at first.

Reading Slider Value

You can read the value of a Slider as selected by the user via the getValue() method. Here is an example of reading the selected value of a JavaFX Slider:

double value = slider.getValue();

Major Tick Unit

You can set the major tick unit of a JavaFX Slider control. The major tick unit is how many units the value changes every time the user moves the handle of the Slider one tick. Here is an example that sets the major tick unit of a JavaFX Slider to 8:

Slider slider = new Slider(0, 100, 0);
slider.setMajorTickUnit(8.0);

This Slider will have its value change with 8.0 up or down whenever the handle in the Slider is moved.

Minor Tick Count

You can set the minor tick count of a JavaFX Slider via the setMinorTickCount() method. The minor tick count specifies how many minor ticks there are between two of the major ticks. Here is an example that sets the minor tick count to 2:

Slider slider = new Slider(0, 100, 0);
slider.setMajorTickUnit(8.0);
slider.setMinorTickCount(3);

The Slider configured here has 8.0 value units between each major tick, and in between each of these major ticks it has 3 minor ticks.

Snap Handle to Ticks

You can make the handle of the JavaFX Slider snap to the ticks using the Slider setSnapToTicks() method, passing a parameter value of true it. Here is an example of making the JavaFX Slider snap its handle to the ticks:

slider.setSnapToTicks(true);

Show Tick Marks

You can make the JavaFX Slider show marks for the ticks when it renders the slider. You do so using its setShowTickMarks() method. Here is an example of making a JavaFX Slider show tick marks:

slider.setShowTickMarks(true);

Here is a screenshot of how a JavaFX Slider looks with tick marks shown:

Show Tick Labels

You can make the JavaFX Slider show tick labels for the ticks when it renders the slider. You do so using its setShowTickLabels() method. Here is an example of making a JavaFX Slider show tick labels:

slider.setShowTickLabels(true);

Here is a screenshot of how a JavaFX Slider looks with tick marks and labels shown:

一个多项属性设置的示例:

package tutorial.slider;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**************************************************************************************************
 @Copyright 2003-2023
 @package tutorial.slider
 @date 2023-04-14
 @author qiao wei
 @version 1.0
 @brief TODO
 @history
 *************************************************************************************************/
public class SliderExample extends Application {

    public static void main(String[] args) {
        Application.launch(SliderExample.class, args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        start01(primaryStage);
    }
    
    private void start01(Stage primaryStage) {
        primaryStage.setTitle("JavaFX App");
        
        Slider slider = new Slider(0, 30, 1);
        // 指定任意两个主刻度之间的次刻度数为1
        slider.setMajorTickUnit(1);
        
        // 指定主刻度之间的副刻度数为0,即没有副刻度
        slider.setMinorTickCount(0);
        
        // 指定当用户点击轨道时拇指移动的距离
        slider.setBlockIncrement(1);
        
        // 保持滑块的值与刻度标记对齐
        slider.setSnapToTicks(true);
        
        // 设置刻度标记标签和刻度标记
        slider.setShowTickLabels(true);
        slider.setShowTickMarks(true);
        
        VBox vBox = new VBox(slider);
        Scene scene = new Scene(vBox, 960, 600);
        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部