[英]Circle won't move position in JavaFX
我正在创建一个小部件,到目前为止,该小部件具有电池监视器和时钟。 我试图在电池监视器后面添加一个圆圈,使其看起来像电池形状。 我试图更改圆的位置值,但它没有移动。 下图显示了我现在所得到的以及预期的效果
这是我的圈子代码:
stack = new StackPane();
Text text2 = new Text();
Circle circle = new Circle(10, 10, 10, Color.BLACK);
batteryBar = new ProgressBar();
batteryBar.setPrefHeight(27);
batteryBar.setId("battery");
stack.getChildren().addAll(batteryBar, text2, circle);
text2.setFont(new Font("Arial", 15));
text2.setStyle("-fx-font-weight: bold;");
text2.setFill(Color.WHITE);
您可以使用setX()
和setY()
方法轻松设置Circle的X和Y坐标。 您可能还需要考虑setLayoutX()
和setLayoutY()
,它们设置相对于Layout
的x
和y
坐标
另请注意,此处不能使用StackPane
。 您必须使用“ Pane
来设置“圆”的x
和y
轴。
这是因为StackPane
用其自己的值覆盖了所有x
和y
值。 要了解更多信息,请查看此堆栈溢出问题
这是您的最终代码:-
pane= new Pane();
Text text2 = new Text();
Circle circle = new Circle(10, 10, 10, Color.BLACK);
circle.setLayoutX(10);
circle.setLayoutY(10);
batteryBar = new ProgressBar();
batteryBar.setPrefHeight(27);
batteryBar.setId("battery");
pane.getChildren().addAll(batteryBar, text2, circle);
text2.setFont(new Font("Arial", 15));
text2.setStyle("-fx-font-weight: bold;");
text2.setFill(Color.WHITE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.