本文介绍了如何在JavaFx中添加ScrollBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将ScrollBar添加到HBox。 ScrollBar被添加,但我没有滚动。我怎样才能使它工作?
I'm trying to add a ScrollBar to a HBox. The ScrollBar gets added, but I get no scrolling. How can I make it work?
public class ScrollableItems {
public void scrollableItems(HBox content) {
double height = 180;
ScrollBar sc = new ScrollBar();
content.getChildren().add(sc);
sc.setLayoutX(content.getWidth() - sc.getWidth());
sc.setMin(0);
sc.setOrientation(Orientation.VERTICAL);
sc.setPrefHeight(height);
sc.setMax(height * 2);
sc.valueProperty().addListener(new ChangeListener<Number>() {
public void changed(ObservableValue<? extends Number> ov,
Number old_val, Number new_val) {
content.setLayoutY(-new_val.doubleValue());
}
});
}
}
将孩子添加到HBox然后将其传递给 scrollableItems(HBox内容)
以上添加SCrollBar
Add children to a HBox then pass it to scrollableItems(HBox content)
above to add a SCrollBar
public HBox mainItemsWrapper() {
HBox scrollabelWrapper = new HBox();
scrollabelWrapper.setMaxHeight(180);
HBox entityDetailViewWrapper = new HBox();
entityDetailViewWrapper.getChildren().addAll(.....);
scrollabelWrapper.getChildren().add(entityDetailViewWrapper);
new ScrollableItems().scrollableItems(scrollabelWrapper);
return scrollabelWrapper;
}
谢谢大家......
Thank you all.....
推荐答案
我真的不明白你为什么要重新发明轮子......你应该使用。
I do not really get why you are trying to reinvent the wheel.. you should probably use the ScrollPane
instead.
这个小例子展示了如何用 ScrollPane
类创建一个可水平滚动的HBox:
This little example shows how to create a horizontally scrollable HBox with the ScrollPane
class:
@Override
public void start(Stage primaryStage) {
HBox hbox = new HBox();
Button b = new Button("add");
b.setOnAction(ev -> hbox.getChildren().add(new Label("Test")));
ScrollPane scrollPane = new ScrollPane(hbox);
scrollPane.setFitToHeight(true);
BorderPane root = new BorderPane(scrollPane);
root.setPadding(new Insets(15));
root.setTop(b);
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
这篇关于如何在JavaFx中添加ScrollBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!