我有一个非常简单的标签,带有文字" 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>
MapCanvas类:
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);
}
}
Generic JavaFX Main class:
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);
}
}