How to center a ImageView node within Region in JavaFx?

杀马特。学长 韩版系。学妹 提交于 2020-01-15 10:53:29

问题


I'm having trouble with centering an ImageView node within the following Browser object. I have provided a snippet of the code below. My thought was to use setStyle("....."), but that didn't appear to have any influence on the placement of the image (It is still displayed top left corner). Any suggestions are greatly appreciated. Thanks!

class Browser extends Region {

    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();
    File file;
    ImageView image;

    public Browser(String fileName, Image loaderImage) throws MalformedURLException {
        file = new File(fileName);

        //apply the styles
        getStyleClass().add("browser");

        //add the web view to the scene
        getChildren().add(browser);

        // load default image
        image = new ImageView();
        image.setImage(loaderImage);
        setStyle("-fx-background-position: CENTER;");
        getChildren().add(image);

    }

回答1:


According to the Javadocs:

By default a Region inherits the layout behavior of its superclass, Parent, which means that it will resize any resizable child nodes to their preferred size, but will not reposition them. If an application needs more specific layout behavior, then it should use one of the Region subclasses: StackPane, HBox, VBox, TilePane, FlowPane, BorderPane, GridPane, or AnchorPane.

So will probably have better luck using one of these.



来源:https://stackoverflow.com/questions/19919719/how-to-center-a-imageview-node-within-region-in-javafx

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