简体   繁体   English

如何在JavaFx的Region中居中放置ImageView节点?

[英]How to center a ImageView node within Region in JavaFx?

I'm having trouble with centering an ImageView node within the following Browser object. 我在以下浏览器对象中居中放置ImageView节点时遇到麻烦。 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). 我的想法是使用setStyle(“ .....”),但这似乎对图像的位置没有任何影响(它仍显示在左上角)。 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);

    }

According to the Javadocs : 根据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. 默认情况下,Region继承其父类Parent的布局行为,这意味着它将将任何可调整大小的子节点的大小调整为它们的首选大小,但不会对其进行重新定位。 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 . 如果应用程序需要更特定的布局行为,则应使用Region子类之一: StackPaneHBoxVBoxTilePaneFlowPaneBorderPaneGridPaneAnchorPane

So will probably have better luck using one of these. 因此使用其中一种可能会带来更好的运气。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM