繁体   English   中英

在不使用BorderPane的情况下设置节点的对齐方式?

[英]Setting the alignment of a node without using BorderPane?

所以我有一个Text节点,我想将其放置在场景的右上角。 大多数方法状态下使用BorderPane ,但在我的情况下,一些在我的场景中的其他节点使用的Pane ,这也是我的根窗格,所以明显的解决方法是添加你想要的节点BorderPane和添加BorderPane到根Pane

但是BorderPane似乎与Pane不兼容。 也就是说,不会显示BorderPane所有节点。 此外, BorderPane也不支持基于节点的转换X和Y定位节点。(即setLayoutX()不起作用。)因此,我什至无法将根PaneBorderPane

这是我尝试过的:

public class foo {
    private Pane root = new Pane();
    private BorderPane borderPane = new BorderPane();
    private Scene scene = new Scene(root, 1366, 768);

    public foo(){
        Text text1 = new Text("test1");
        text1.setLayoutX(11); // Ignore this if you want.
        test1.setLayoutY(11);

        Text text2 = new Text("test2");

        root.getChildren().add(test1);
        borderPane.setRight(text2);
        root.getChildren().add(borderPane);

        // Display the scene on a stage.
    }
}

这里只显示了test1 ,没有看到test2 有没有一种方法可以在使用BorderPane 情况下将此文本放置在屏幕的BorderPane

Pane仅将子级调整为首选大小。 由于仅将Text节点作为BorderPane子节点放置,因此其首选大小是该子节点的大小。

在这种情况下,我建议使用其他布局,例如

StackPane

这允许您使用StackPane.setAlignment(child, newAlignment);在布局内设置节点的对齐方式StackPane.setAlignment(child, newAlignment); 缺点是,设置绝对位置要求您要么使子级不受管理(有一些副作用),要么指定4个值( Insets实例)作为边距。

Text text1 = new Text("test1");
StackPane.setMargin(text1, new Insets(11, 0, 0, 11));
StackPane.setAlignment(text1, Pos.TOP_LEFT);

Text text2 = new Text("test2");
StackPane.setAlignment(text2, Pos.TOP_RIGHT);

root = new StackPane(text1, text2);

锚板

这不允许中心对齐,但是它允许您像在Pane一样使用layoutXlayoutY设置绝对位置。 您可以设置锚点属性以指定孩子到顶部,左侧,右侧和/或底部的距离:

Text text1 = new Text("test1");
text1.setLayoutX(11);
text1.setLayoutY(11);

Text text2 = new Text("test2");
AnchorPane.setRightAnchor(text2, 0d);
AnchorPane.setTopAnchor(text2, 0d);

root = new AnchorPane(text1, text2);

暂无
暂无

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

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