Insert text at the center of a image

泪湿孤枕 提交于 2019-12-07 08:00:42

问题


I have this code which inserts Image into the center of a borderpane:

private static final ImageView iv;    
    static {
        iv = new ImageView(StartPanel.class.getResource("/com/dx57dc/images/6.jpg").toExternalForm());
    }

bpi.setCenter(iv);

And now I have this problem. I have this insert text as a label at the center of the image.

Text inftx = new Text("Infrastructure");

How I can do this?


回答1:


You can use a StackPane to performed the desired image overlap. The following code can be used on your example:

private static final ImageView iv;    

static {
    iv = new ImageView(StartPanel.class.getResource("/com/dx57dc/images/6.jpg").toExternalForm());
    Text inftx = new Text("Infrastructure");
    StackPane pane = new StackPane();

    pane.getChildren().add(iv);
    pane.getChildren().add(inftx);

    pane.setAlignment(Pos.CENTER);
}

As informed on StackPane tutorial:

The StackPane layout pane places all of the nodes within a single stack with each new node added on top of the previous node. This layout model provides an easy way to overlay text on a shape or image or to overlap common shapes to create a complex shape.



来源:https://stackoverflow.com/questions/18165662/insert-text-at-the-center-of-a-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!