AnchorPane Constraints in percentages

ⅰ亾dé卋堺 提交于 2019-12-11 04:46:04

问题


I am working on a project and I need you help.
I want to know that is this possible to set anchorpane constraints in percentage as something like

AnchorPane.setLeftAnchor(content1, 35%);

回答1:


Yes it can be by updating constraint values on every scene size change:

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);
    }

}


来源:https://stackoverflow.com/questions/24172228/anchorpane-constraints-in-percentages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!