我在一个舞台上添加一个滚动条,我想修复舞台右侧的滚动条,起初我想尝试通过css,但我找不到类似"的位置:绝对& #34;,所以我在舞台上添加一个ChangeListener,当舞台调整大小时,重置滚动条的LayoutX和LayoutY,是的,它可以工作,但是当我最大化舞台时,它不起作用,我在舞台上添加三个监听器
stage.heightProperty().addListener(new ChangeListener<Number>()
{
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue)
{
System.out.println("this is height,newValue:"+newValue.intValue()+"oldValue:"+oldValue.intValue());
}
});
stage.maximizedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
System.out.println("current height"+stage.heightProperty().doubleValue());
System.out.println("current width"+stage.widthProperty().doubleValue());
}
});
stage.widthProperty().addListener(new ChangeListener<Number>()
{
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue)
{
hBox.setPrefWidth(newValue.intValue()-30);
table1HorizontalScrollBar = findScrollBar( table1, Orientation.HORIZONTAL);
table1VerticalScrollBar = findScrollBar( table1, Orientation.VERTICAL);
table2.setPrefWidth(newValue.intValue()-table1.getWidth());
table2HorizontalScrollBar = findScrollBar( table2, Orientation.HORIZONTAL);
table2VerticalScrollBar = findScrollBar( table2, Orientation.VERTICAL);
bindValueProperty();
System.out.println("this is width ,newValue:"+newValue.intValue()+"oldValue:"+oldValue.intValue());
}
});
当我最大化舞台时,控制台输出如下所示:
现在,我可以获得舞台的宽度和高度的新值; 但是在舞台上,卷轴仍然处于旧位置,就像这样
答案 0 :(得分:0)
只需为此作业使用正确的布局:
@Override
public void start(Stage primaryStage) {
ScrollBar scrollBar = new ScrollBar();
scrollBar.setOrientation(Orientation.VERTICAL);
AnchorPane root = new AnchorPane(scrollBar);
AnchorPane.setTopAnchor(scrollBar, 0d);
AnchorPane.setRightAnchor(scrollBar, 0d);
AnchorPane.setBottomAnchor(scrollBar, 0d);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
或2 ScrollBar
s:
@Override
public void start(Stage primaryStage) {
ScrollBar vScrollBar = new ScrollBar();
vScrollBar.setOrientation(Orientation.VERTICAL);
ScrollBar hScrollBar = new ScrollBar();
hScrollBar.setOrientation(Orientation.HORIZONTAL);
Region r = new Region();
r.setBackground(new Background(new BackgroundFill(Color.BLUE, CornerRadii.EMPTY, Insets.EMPTY)));
r.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, BorderWidths.DEFAULT)));
GridPane root = new GridPane();
ColumnConstraints c1 = new ColumnConstraints();
c1.setHgrow(Priority.ALWAYS);
ColumnConstraints c2 = new ColumnConstraints();
c2.setHgrow(Priority.NEVER);
root.getColumnConstraints().addAll(c1, c2);
RowConstraints r1 = new RowConstraints();
r1.setVgrow(Priority.ALWAYS);
RowConstraints r2 = new RowConstraints();
r2.setVgrow(Priority.NEVER);
root.getRowConstraints().addAll(r1, r2);
root.addRow(0, r, vScrollBar);
root.add(hScrollBar, 0, 1);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}