[英]Setting the alignment of a node without using BorderPane?
所以我有一个Text节点,我想将其放置在场景的右上角。 大多数方法状态下使用BorderPane
,但在我的情况下,一些在我的场景中的其他节点使用的Pane
,这也是我的根窗格,所以明显的解决方法是添加你想要的节点BorderPane
和添加BorderPane
到根Pane
。
但是BorderPane
似乎与Pane
不兼容。 也就是说,不会显示BorderPane
所有节点。 此外, BorderPane
也不支持基于节点的转换X和Y定位节点。(即setLayoutX()不起作用。)因此,我什至无法将根Pane
为BorderPane
。
这是我尝试过的:
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.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
一样使用layoutX
和layoutY
设置绝对位置。 您可以设置锚点属性以指定孩子到顶部,左侧,右侧和/或底部的距离:
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.