[英]Circle won't move position in JavaFX
I am creating a widget which so far has a battery monitor and clock. 我正在创建一个小部件,到目前为止,该小部件具有电池监视器和时钟。 I tried to add a circle behind the battery monitor to make it look like the shape of a battery. 我试图在电池监视器后面添加一个圆圈,使其看起来像电池形状。 I have tried to change the position values of the circle but it isn't moving. 我试图更改圆的位置值,但它没有移动。 the following picture shows what I am getting now and the intended effect 下图显示了我现在所得到的以及预期的效果
Here is my code for the circle: 这是我的圈子代码:
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);
You can Easily set the X and Y coordinates of the Circle with the methods setX()
and setY()
. 您可以使用setX()
和setY()
方法轻松设置Circle的X和Y坐标。 You also might want to consider setLayoutX()
and setLayoutY()
which set the x
and y
coordinates relative to the Layout
您可能还需要考虑setLayoutX()
和setLayoutY()
,它们设置相对于Layout
的x
和y
坐标
Also Note that StackPane
cannot be used here. 另请注意,此处不能使用StackPane
。 You must use Pane
for setting the x
and y
axis of the Circle. 您必须使用“ Pane
来设置“圆”的x
和y
轴。
This is because StackPane
overrides all the x
and y
values with its own. 这是因为StackPane
用其自己的值覆盖了所有x
和y
值。 To know more about this, check out this Stack-overflow question 要了解更多信息,请查看此堆栈溢出问题
Here would be your final Code :- 这是您的最终代码:-
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.