JavaFX通过xml和通过java文件构建

JavaFX通过xml和通过java文件构建

通过xml,使用Parent和其子类效果一样

public void start(Stage primaryStage) throws Exception{
    //使用Parent,生成的界面是居中的。
    Parent root = FXMLLoader.load(getClass().getResource("/HBoxMain.fxml"));
    primaryStage.setTitle("HBox Button 自动增长");
    primaryStage.setScene(new Scene(root));
    primaryStage.show();
 }

通过java文件:建议使用Parent的子类。使用parent和其子类效果不一样

//生成的界面不够居中,Parent没有setPrefSize()方法
public void start(Stage primaryStage) throws Exception {
    //使用BorderPane作为根面板
    Parent root = new BorderPane();
    Scene scene = new Scene(root);
    root.prefWidth(1200.0);
    root.prefHeight(800.0);
    primaryStage.setScene(scene);
    primaryStage.setTitle("Layout Sample");
    primaryStage.show();
}


//生成的界面居中,因为BorderPane有setPrefSize()方法
public void start(Stage primaryStage) throws Exception {
    //使用BorderPane作为根面板
    BorderPane root = new BorderPane();
    Scene scene = new Scene(root);
    root.setPrefSize(1200.0,800.0);
    primaryStage.setScene(scene);
    primaryStage.setTitle("Layout Sample");
    primaryStage.show();
}

猜你喜欢

转载自blog.csdn.net/cdc_csdn/article/details/80722370