ScrollBar類經(jīng)常帶有一個可滾動的窗格。
import javafx.application.Application; import javafx.geometry.Orientation; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ScrollBar; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage stage) { Group root = new Group(); Scene scene = new Scene(root, 500, 200); stage.setScene(scene); ScrollBar s1 = new ScrollBar(); root.getChildren().add(s1); stage.show(); } public static void main(String[] args) { launch(args); } }
滾動條有四個區(qū)域:
上面的代碼生成以下結(jié)果。
以下代碼使用其默認構(gòu)造函數(shù)創(chuàng)建滾動條。
ScrollBar sc = new ScrollBar();
setMin和setMax方法定義滾動條表示的最小值和最大值。
setValue方法設(shè)置滾動的當前值,也設(shè)置拇指的位置。
sc.setMin(0); sc.setMax(100); sc.setValue(50);
當用戶移動縮略圖時,滾動條的值會更改。
默認情況下,滾動條水平定向。我們可以使用 setOrientation
方法設(shè)置垂直方向。
我們可以單擊左右按鈕的水平滾動條或向上和向下按鈕垂直滾動條滾動一個單位增量。UNIT_INCREMENT屬性設(shè)置此值。
單擊軌道可使?jié)L動條移動塊增量。BLOCK_INCREMENT屬性定義此值。
import javafx.application.Application; import javafx.geometry.Orientation; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ScrollBar; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage stage) { Group root = new Group(); Scene scene = new Scene(root, 500, 200); stage.setScene(scene); ScrollBar s1 = new ScrollBar(); s1.setMax(500); s1.setMin(0); s1.setValue(100); s1.setUnitIncrement(30); s1.setBlockIncrement(35); s1.setOrientation(Orientation.VERTICAL); root.getChildren().add(s1); stage.show(); } public static void main(String[] args) { launch(args); } }
上面的代碼生成以下結(jié)果。
以下代碼為滾動事件從滾動條添加事件處理程序。
import javafx.application.Application; import javafx.beans.value.ObservableValue; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ScrollBar; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage stage) { Group root = new Group(); Scene scene = new Scene(root, 500, 200); stage.setScene(scene); ScrollBar s1 = new ScrollBar(); s1.valueProperty().addListener((ObservableValue<? extends Number> ov, Number old_val, Number new_val) -> { System.out.println(-new_val.doubleValue()); }); root.getChildren().add(s1); stage.show(); } public static void main(String[] args) { launch(args); } }
上面的代碼生成以下結(jié)果。
更多建議: