簡體   English   中英

JavaFX中的Parent與BorderPane

[英]Parent vs BorderPane in JavaFX

使用Parent來定義場景而不是BorderPaneAnchor等是一個很好的實踐嗎?

以這種方式使用Parent的好處是什么?

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("Main.fxml")); //Insted of this:   BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Main.fxml"));
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

這是很好的做法,因為Parent只是一個Node ,可以有子節點( Parent是子類Node )。

通過使用Parent而不是BorderPane ,您使用的是更通用的類型,而不是將其綁定到某種類型的窗格。 這樣做的原因與將ArrayList聲明為List類型的對象的原因相同。 如果明天要加載VBox ,則無需修改代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM