如何在JavaFX场景中定位按钮(或任何GUI元素)?

Mil*_*vic 13 user-interface position javafx button fxml

我试图将UIFX按钮放在UI上的特定位置(特定坐标),但没有任何工作.我猜有一个方法用于此,但我找不到它.

Vin*_* CG 22

你可以使用窗格.setLayoutX()和setLayoutY().

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;

public class Tester extends Application {


public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("Hello World!");
    Button btn = new Button();
    btn.setText("'Hello World'");
    btn.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            System.out.println("Hello World!");
        }
    });

    Pane root = new Pane();
    btn.setLayoutX(250);
    btn.setLayoutY(220);
    root.getChildren().add(btn);
    primaryStage.setScene(new Scene(root, 300, 250));
    primaryStage.show();
}
}
Run Code Online (Sandbox Code Playgroud)