Original address http://download.oracle.com/javafx/2.0/ui_controls/slider.htm
Slider
Class to display and respond to the value of a range. This control includes a track and a slide that can be dragged, as well as a scale and scale mark to indicate a value. Figure
15-1 shows a slide and shows its main elements.
Figure 15-1 Elements of a slider
Description of "Figure 15-1 Elements of a slider"
Create a slider
Take a moment to look at the code in example 15-1, which generates a figure
15-1 slide bar.
Example 15-1 Creating a slider
Slider slider = new Slider();slider.setMin(0);slider.setMax(100);slider.setValue(40);slider.setShowTickLabels(true);slider.setShowTickMarks(true);slider.setMajorTickUnit(50);slider.setMinorTickCount(5);slider.setBlockIncrement(10);
setMin
And
setMax
The method distribution defines the minimum and maximum values of the slide bar.The setvalue method specifies the current value of the slide, which must be between the minimum and maximum values.
This method is used to define the position of the slide mark after the application is started.
Two Boolean MethodssetShowTickMarks
AndsetShowTickLabels
Defines the visual appearance of the slide bar. In Example
In 15-1, the scale and value are displayed. In addition, the Unit distance between large scales is set to 50, and the number of small scales is defined as 5. SetSet the setsnaptoticks method
true
To keep the scale of the slide mark.
The setblockincrement method defines the moving distance of sliding labels when a user clicks a track.
Example
In 15-1, the value is 10. That is to say, when a user clicks the track, the slide moves 10 units to the click direction.
Use slide bars in graphic applications
Now let's take a look at figure 15-2. This application uses three slide entries to whitelist the attributes of several images. Each slide adjusts a specific Visual Feature: transparency, brown adjustment, and scaling ratio.
Figure 15-2 three sdocumers
Description of "Figure 15-2 three sdocumers"
Example 15-2 shows the source code of this application.
Example 15-2 slider sample
import javafx.application.Application;import javafx.beans.value.ChangeListener;import javafx.beans.value.ObservableValue;import javafx.geometry.Insets;import javafx.scene.Group;import javafx.scene.Scene;import javafx.scene.control.Label;import javafx.scene.control.Slider;import javafx.scene.effect.SepiaTone;import javafx.scene.image.Image;import javafx.scene.image.ImageView;import javafx.scene.layout.GridPane;import javafx.scene.paint.Color;import javafx.stage.Stage; public class Main extends Application { final Slider opacityLevel = new Slider(0, 1, 1); final Slider sepiaTone = new Slider(0, 1, 1); final Slider scaling = new Slider (0.5, 1, 1); final Image image = new Image(getClass().getResourceAsStream( "cappuccino.jpg") ); final Label opacityCaption = new Label("Opacity Level:"); final Label sepiaCaption = new Label("Sepia Tone:"); final Label scalingCaption = new Label("Scaling Factor:"); final Label opacityValue = new Label( Double.toString(opacityLevel.getValue())); final Label sepiaValue = new Label( Double.toString(sepiaTone.getValue())); final Label scalingValue = new Label( Double.toString(scaling.getValue())); final static Color textColor = Color.WHITE; final static SepiaTone sepiaEffect = new SepiaTone(); @Override public void start(Stage stage) { Group root = new Group(); Scene scene = new Scene(root, 600, 400); stage.setScene(scene); stage.setTitle("Slider Sample"); scene.setFill(Color.BLACK); GridPane grid = new GridPane(); grid.setPadding(new Insets(10, 10, 10, 10)); grid.setVgap(10); grid.setHgap(70); final ImageView cappuccino = new ImageView (image); cappuccino.setEffect(sepiaEffect); GridPane.setConstraints(cappuccino, 0, 0); GridPane.setColumnSpan(cappuccino, 3); grid.getChildren().add(cappuccino); scene.setRoot(grid); opacityCaption.setTextFill(textColor); GridPane.setConstraints(opacityCaption, 0, 1); grid.getChildren().add(opacityCaption); opacityLevel.valueProperty().addListener(new ChangeListener<Number>() { public void changed(ObservableValue<? extends Number> ov, Number old_val, Number new_val) { cappuccino.setOpacity(new_val.doubleValue()); opacityValue.setText(String.format("%.2f", new_val)); } }); GridPane.setConstraints(opacityLevel, 1, 1); grid.getChildren().add(opacityLevel); opacityValue.setTextFill(textColor); GridPane.setConstraints(opacityValue, 2, 1); grid.getChildren().add(opacityValue); sepiaCaption.setTextFill(textColor); GridPane.setConstraints(sepiaCaption, 0, 2); grid.getChildren().add(sepiaCaption); sepiaTone.valueProperty().addListener(new ChangeListener<Number>() { public void changed(ObservableValue<? extends Number> ov, Number old_val, Number new_val) { sepiaEffect.setLevel(new_val.doubleValue()); sepiaValue.setText(String.format("%.2f", new_val)); } }); GridPane.setConstraints(sepiaTone, 1, 2); grid.getChildren().add(sepiaTone); sepiaValue.setTextFill(textColor); GridPane.setConstraints(sepiaValue, 2, 2); grid.getChildren().add(sepiaValue); scalingCaption.setTextFill(textColor); GridPane.setConstraints(scalingCaption, 0, 3); grid.getChildren().add(scalingCaption); scaling.valueProperty().addListener(new ChangeListener<Number>() { public void changed(ObservableValue<? extends Number> ov, Number old_val, Number new_val) { cappuccino.setScaleX(new_val.doubleValue()); cappuccino.setScaleY(new_val.doubleValue()); scalingValue.setText(String.format("%.2f", new_val)); } }); GridPane.setConstraints(scaling, 1, 3); grid.getChildren().add(scaling); scalingValue.setTextFill(textColor); GridPane.setConstraints(scalingValue, 2, 3); grid.getChildren().add(scalingValue); stage.show(); } public static void main(String[] args) { launch(args); }}
ImageView
The opacity of an object is changed by the first slide, called opacitylevel.。SepiaTone
The effect change is controlled by the sepiatone slider. The third slide defines the magnification, which callssetScaleX
And
setScaleY
Method.
The code in example 15-3 isSlider class
The double-precision value returned by the getvalue method is converted
String
The format is also used to display the value of the slider bar: The floating point type of the last two decimal places.
Example 15-3 formatting the rendered Slider's Value
scalingValue.setText((Double.toString(value)).format("%.2f", value));
The next step is to apply visual effects or CSS styles to improve the appearance and feeling.