2016-02-13 101 views
0

使用Parent来定义场景而不是BorderPaneAnchor等是否是一个很好的实践?JavaFX中的Parent vs BorderPane

以这种方式使用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); 
    } 
} 

回答

2

这是很好的做法,因为Parent仅仅是Node,可以有子节点(ParentNode一个子类)。

通过使用Parent而不是BorderPane,您正在使用更一般的类型,而不是将其绑定到某种类型的窗格。您这样做的原因与您将ArrayList声明为List类型的对象的原因相同。如果明天你正在加载一个VBox,你不需要修改你的代码。