JavaFX |滑块类
Slider 是 JavaFX 中的控件,用于显示有效数字选择的连续或离散范围,并允许用户与控件进行交互。滑块呈现为带有旋钮的垂直或水平条,用户可以滑动该旋钮以指示所需的值。滑块也可以有刻度线和标签来指示沿条的间隔。
滑块的三个基本变量是min 、 max和value 。该值应始终是由 min 和 max 定义的范围内的数字。 min应始终小于到max 。 min默认为 0,而max默认为 100。
类的构造函数:
- Slider():创建一个默认的 Slider 实例。
- Slider(double min, double max, double value):构造一个具有指定滑块最小值、最大值和当前值的滑块控件。
常用方法:Method Description adjustValue(double newValue) Adjusts value to match newValue. decrement() Decrements the value by blockIncrement, bounded by max. getBlockIncrement() Gets the value of the property blockIncrement. getMax() Gets the value of the property max. getMin() Gets the value of the property min. getMajorTickUnit() Gets the value of the property majorTickUnit. getMinorTickCount() Gets the value of the property minorTickCount. getValue() Gets the value of the property value. increment() Increments the value by blockIncrement, bounded by max. setBlockIncrement(double value) Sets the value of the property blockIncrement. setMajorTickUnit(double value) Sets the value of the property majorTickUnit. setMax(double value) Sets the value of the property max. setMin(double value) Sets the value of the property min. setMinorTickCount(int value) Sets the value of the property minorTickCount. setValue(double value) Sets the value of the property value. setValueChanging(boolean value) Sets the value of the property valueChanging. setShowTickMarks(boolean value) Sets the value of the property showTickMarks. setShowTickLabels(boolean value) Sets the value of the property showTickLabels. isShowTickLabels() Gets the value of the property showTickLabels. isShowTickMarks() Gets the value of the property showTickMarks.
下面的程序说明了 Slider 类的使用:
- 实现 Slider 类的简单Java程序:在这个程序中,我们将创建一个组和场景。将场景添加到框架中。然后,创建一个 Slider 并将其添加到框架中。现在启动应用程序。
Java
// Java program to implement the Slider Class
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.stage.Stage;
public class SliderExample extends Application {
public void start(Stage stage)
{
// creating group
Group root = new Group();
Scene scene = new Scene(root, 600, 400);
// set Scene to the stage
stage.setScene(scene);
// set title for the frame
stage.setTitle("Slider Sample");
// create slider
Slider slider = new Slider();
// add slider to the frame
root.getChildren().add(slider);
stage.show();
}
// Main Method
public static void main(String[] args)
{
// launch the application
launch(args);
}
}
Java
// Java program to implement Slider class
// by using TickMarks and TickLabels
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.stage.Stage;
public class SliderExample extends Application {
public void start(Stage stage)
{
Group root = new Group();
// create a Scene
Scene scene = new Scene(root, 600, 400);
// add Scene to the frame
stage.setScene(scene);
// set title of the frame
stage.setTitle("Slider Sample");
// Creates a slider
Slider slider = new Slider(0, 1, 0.5);
// enable the marks
slider.setShowTickMarks(true);
// enable the Labels
slider.setShowTickLabels(true);
// set Major tick unit
slider.setMajorTickUnit(0.25f);
// sets the value of the property
// blockIncrement
slider.setBlockIncrement(0.1f);
root.getChildren().add(slider);
// display
stage.show();
}
// Main Method
public static void main(String[] args)
{
// Launch the application
launch(args);
}
}
Java
// Java program to implement Slider Class
// using ChangeListener
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
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.scene.paint.Color;
import javafx.stage.Stage;
public class SliderExample extends Application {
public void start(Stage stage)
{
// create label
Label label = new Label("Select the Brightness");
Label l = new Label(" ");
// set the color of the text
l.setTextFill(Color.BLACK);
// create slider
Slider slider = new Slider();
// set the value of property min,
// max and value
slider.setMin(0);
slider.setMax(100);
slider.setValue(80);
// enable TickLabels and Tick Marks
slider.setShowTickLabels(true);
slider.setShowTickMarks(true);
slider.setBlockIncrement(10);
// Adding Listener to value property.
slider.valueProperty().addListener(
new ChangeListener() {
public void changed(ObservableValue extends Number >
observable, Number oldValue, Number newValue)
{
l.setText("value: " + newValue);
}
});
// create a VBox
VBox root = new VBox();
root.setPadding(new Insets(20));
root.setSpacing(10);
root.getChildren().addAll(label, slider, l);
stage.setTitle("Slider Sample");
// create Scene and add to the frame
Scene scene = new Scene(root, 350, 200);
stage.setScene(scene);
stage.show();
}
// Main Method
public static void main(String[] args)
{
// Launch Application
Application.launch(args);
}
}
输出:
- 使用 TickMarks 和 TickLabels 实现 Slider 类的Java程序:在这个程序中,我们将创建一个组和场景。将场景添加到框架中。创建具有指定min 、 max和value的滑块。启用标记和标签。将MajorTickUnit设置为指定的值。将 Slider 添加到框架并显示它。
Java
// Java program to implement Slider class
// by using TickMarks and TickLabels
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.stage.Stage;
public class SliderExample extends Application {
public void start(Stage stage)
{
Group root = new Group();
// create a Scene
Scene scene = new Scene(root, 600, 400);
// add Scene to the frame
stage.setScene(scene);
// set title of the frame
stage.setTitle("Slider Sample");
// Creates a slider
Slider slider = new Slider(0, 1, 0.5);
// enable the marks
slider.setShowTickMarks(true);
// enable the Labels
slider.setShowTickLabels(true);
// set Major tick unit
slider.setMajorTickUnit(0.25f);
// sets the value of the property
// blockIncrement
slider.setBlockIncrement(0.1f);
root.getChildren().add(slider);
// display
stage.show();
}
// Main Method
public static void main(String[] args)
{
// Launch the application
launch(args);
}
}
输出 :
- 使用 ChangeListener 实现 Slider 类的Java程序:在这个程序中,我们将创建一个标签并设置文本的颜色。创建一个滑块并设置它的最小值、最大值和值。启用 TickLabels 和 TickMarks。设置属性blockIncrement的值。 setBlockIncrement()方法定义用户点击轨道时拇指移动的距离。添加 ChangeListener,在移动滑块时,亮度变化的值将显示在标签中。创建一个 VBox 并添加到框架中。创建场景和框架。最后,启动应用程序。
Java
// Java program to implement Slider Class
// using ChangeListener
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
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.scene.paint.Color;
import javafx.stage.Stage;
public class SliderExample extends Application {
public void start(Stage stage)
{
// create label
Label label = new Label("Select the Brightness");
Label l = new Label(" ");
// set the color of the text
l.setTextFill(Color.BLACK);
// create slider
Slider slider = new Slider();
// set the value of property min,
// max and value
slider.setMin(0);
slider.setMax(100);
slider.setValue(80);
// enable TickLabels and Tick Marks
slider.setShowTickLabels(true);
slider.setShowTickMarks(true);
slider.setBlockIncrement(10);
// Adding Listener to value property.
slider.valueProperty().addListener(
new ChangeListener() {
public void changed(ObservableValue extends Number >
observable, Number oldValue, Number newValue)
{
l.setText("value: " + newValue);
}
});
// create a VBox
VBox root = new VBox();
root.setPadding(new Insets(20));
root.setSpacing(10);
root.getChildren().addAll(label, slider, l);
stage.setTitle("Slider Sample");
// create Scene and add to the frame
Scene scene = new Scene(root, 350, 200);
stage.setScene(scene);
stage.show();
}
// Main Method
public static void main(String[] args)
{
// Launch Application
Application.launch(args);
}
}
输出:
注意:上述程序可能无法在在线 IDE 中运行。请使用离线编译器。
参考: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Slider.html