如何使用 JavaFX 建立一個捲軸?


捲軸包含一個拇指、一個右側按鈕和一個左側按鈕,這些按鈕連線到一個可滾動窗格。使用這些按鈕,可以上下滾動連線到它的窗格。

在 JavaFX 中,javafx.scene.control.ScrollBar 表示一個捲軸。您可以透過例項化這個類來建立一個捲軸。

您可以建立垂直或水平捲軸,預設情況下建立一個水平捲軸,可以透過使用 setOrientation() 方法將其更改為垂直捲軸。

通常,捲軸與其他控制元件(如 ScrollPane、ListView 等)關聯。

示例

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollBar;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class ScrollBarExample extends Application {
   public void start(Stage stage) {
      //Label for education
      Label label = new Label("Educational qualification:");
      Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
      label.setFont(font);
      //list View for educational qualification
      ScrollBar scroll = new ScrollBar();
      scroll.setMin(0);
      scroll.setMax(400);
      scroll.setValue(50);
      //Setting the position of the scroll pane
      scroll.setLayoutX(180);
      scroll.setLayoutY(75);
      //Setting the stage
      Group root = new Group(scroll);
      Scene scene = new Scene(root, 595, 200, Color.BEIGE);
      stage.setTitle("List View Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

輸出

更新日期:2020 年 5 月 18 日

881 次瀏覽

開啟你的事業

完成此課程後獲得認證

開始學習
廣告