如何在JavaFx中的Region中定位一个ImageView节点? [英] How to center a ImageView node within Region in JavaFx?

查看:758
本文介绍了如何在JavaFx中的Region中定位一个ImageView节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在以下浏览器对象中居中一个ImageView节点。我已经提供了下面的代码片段。我的想法是使用setStyle(.....),但是这似乎没有影响图像的位置(它仍然显示在左上角)。任何建议都非常感谢。谢谢!

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

    }


推荐答案

Javadocs


默认情况下,一个区域继承其父类的布局行为, Parent ,这意味着它将调整任何可调整大小的子节点到其首选大小,但不会重新定位它们。如果应用程序需要更具体的布局行为,那么它应该使用 Region 子类之一: StackPane HBox VBox TilePane FlowPane BorderPane GridPane AnchorPane

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.

这篇关于如何在JavaFx中的Region中定位一个ImageView节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆