繁体   English   中英

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

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

我在以下浏览器对象中居中放置ImageView节点时遇到麻烦。 我提供了下面的代码片段。 我的想法是使用setStyle(“ .....”),但这似乎对图像的位置没有任何影响(它仍显示在左上角)。 任何建议,不胜感激。 谢谢!

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

    }

根据Javadocs

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

因此使用其中一种可能会带来更好的运气。

暂无
暂无

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

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