JavaFX issue when placing square in coordinates

so i have the following code from rectangle

rect.setX(10);

and

rect.setY(20);

but whenever I compile the application, the rectangle always moves to the middle of the application frame.

Import Used:

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.shape.*;
import javafx.scene.Scene;
import javafx.geometry.Insets;
import javafx.scene.input.MouseEvent;
import javafx.event.EventHandler;
import javafx.scene.input.KeyCombination;
import javafx.scene.paint.Color;
import javafx.scene.layout.*;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
0
source share
1 answer

Given that the rectangle is added to StackPane, you should use the translate properties to move the rectangle. Try:

rect.setTranslateX(10);
rect.setTranslateY(20);
0
source

All Articles