简体   繁体   中英

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

image

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() . You also might want to consider setLayoutX() and setLayoutY() which set the x and y coordinates relative to the Layout

Also Note that StackPane cannot be used here. You must use Pane for setting the x and y axis of the Circle.

This is because StackPane overrides all the x and y values with its own. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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