0

我有一个非常简单的标签,其中将文本“Hello World”添加到父对象中,我想将其添加到 BorderPane 的中间位置。

标签开始有点偏离中心,当调整窗口大小时,标签/父级“捕捉”到我想要的位置。为什么它会这样?

FXML 类:

<BorderPane fx:id="pane" fx:controller="sample.Controller"
            xmlns:fx="http://javafx.com/fxml">
<center>
    <MapCanvas fx:id="map" BorderPane.alignment="CENTER"/>
</center>
</BorderPane>

地图画布类:

public class MapCanvas extends Parent {
    public MapCanvas() {
        initComponents();
    }

    private void initComponents() {
        Label world = new Label();
        world.setText("Hello world");
        world.setFont(new Font("Comic Sans MS", 24));
        getChildren().add(world);
    }
}

通用 JavaFX 主类:

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
    Model model = new Model();
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    primaryStage.setTitle("Hello World");
    primaryStage.setScene(new Scene(root, 300, 300));
    primaryStage.show();
}

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

0 回答 0