如何使用 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 次浏览

开启你的事业

完成此课程后获得认证

开始学习
广告
© . All rights reserved.