如何在JavaFx中的区域内居中ImageView节点?

时间:2013-11-12 02:20:07

标签: java css javafx

我遇到了将ImageView节点集中在以下Browser对象中的问题。我提供了以下代码的片段。我的想法是使用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);

    }

1 个答案:

答案 0 :(得分:2)

根据Javadocs

  

默认情况下,Region继承其超类Parent的布局行为,这意味着它会将任何可调整大小的子节点的大小调整为其首选大小,但不会重新定位它们。如果应用程序需要更具体的布局行为,那么它应该使用Region子类之一:StackPaneHBoxVBoxTilePane,{{1 }},FlowPaneBorderPaneGridPane

所以使用其中一个可能会有更好的运气。