2011-11-29 77 views

回答

11

沒有CardLayout,但你可以使用TabPane或簡單地切換組:

public void start(Stage stage) { 

    VBox vbox = new VBox(5); 

    Button btn = new Button("1"); 
    Button btn2 = new Button("2"); 

    final Pane cardsPane = new StackPane(); 
    final Group card1 = new Group(new Text(25, 25, "Card 1")); 
    final Group card2 = new Group(new Text(25, 25, "Card 2")); 

    btn.setOnAction(new EventHandler<ActionEvent>() { 
     public void handle(ActionEvent t) { 
      cardsPane.getChildren().clear(); 
      cardsPane.getChildren().add(card1); 
     } 
    }); 

    btn2.setOnAction(new EventHandler<ActionEvent>() { 
     public void handle(ActionEvent t) { 
      cardsPane.getChildren().clear(); 
      cardsPane.getChildren().add(card2); 
     } 
    }); 

    vbox.getChildren().addAll(btn, btn2, cardsPane); 
    stage.setScene(new Scene(vbox)); 
    stage.setWidth(200); 
    stage.setHeight(200); 
    stage.show(); 

} 
+0

我試着用TabPane。但它顯示一個標籤菜單。所以我採用了你在這裏建議的方式。它適合我的要求。感謝Sergey。 :) – Anuruddha

+2

請注意,在2.2版本中有一個名爲'Pagination'的新控件,它可能更適合您的需求。 –

6

另一種選擇是使用StackPane並設置所有的可見性,但CURENT Panefalse。不理想,但另一種思考問題的方式

相關問題