簡體   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