gpt4 book ai didi

java - 以百分比表示的 AnchorPane 约束

转载 作者:行者123 更新时间:2023-11-29 05:22:21 25 4
gpt4 key购买 nike

我正在做一个项目,我需要你的帮助。
我想知道是否可以将 percentage 中的锚定 Pane 约束设置为类似

AnchorPane.setLeftAnchor(content1, 35%);

最佳答案

是的,可以通过在每次场景大小更改时更新约束值来实现:

public class AnchorDemo extends Application {

private final Button button = new Button("Add");
private final ListView list = new ListView();

@Override
public void start(Stage primaryStage) {

AnchorPane root = new AnchorPane();
AnchorPane.setTopAnchor(list, 10.0);
AnchorPane.setLeftAnchor(list, 10.0);
AnchorPane.setTopAnchor(button, 10.0);
AnchorPane.setRightAnchor(button, 10.0);
root.getChildren().addAll(list, button);

Scene scene = new Scene(root, 300, 250);

scene.widthProperty().addListener(new ChangeListener<Number>() {

@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
updateWidthConstaints(newValue.doubleValue());
}
});

primaryStage.setScene(scene);
primaryStage.show();

updateWidthConstaints(scene.getWidth());
}

private void updateWidthConstaints(double width) {
// roughly give to the list 66% while to the button 33% of available
// space, besides paddings.
// +5s are for extra padding
AnchorPane.setRightAnchor(list, width * 1 / 3 + 5);
AnchorPane.setLeftAnchor(button, width * 2 / 3 + 5);
}

public static void main(String[] args) {
launch(args);
}

}

关于java - 以百分比表示的 AnchorPane 约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24172228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com