Circle不会在JavaFX中移动位置

我正在创建一个小部件,到目前为止有一个电池监视器和时钟。 我试图在电池监视器后面添加一个圆圈,使其看起来像电池的形状。 我试图改变圆的位置值,但它没有移动。 下图显示了我现在得到的和预期的效果

image

这是我的圆圈代码:

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坐标。 您还可以考虑设置相对于Layoutxy坐标的setLayoutX()setLayoutY()

另请注意,此处不能使用StackPane 。 您必须使用Pane来设置圆的xy轴。

这是因为StackPane用自己的值覆盖所有xy值。 要了解更多相关信息,请查看此堆栈溢出问题

这将是您的最终代码: –

  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);