在我的javaFX 2.0應用程序中,我需要替換使用awt.CardLayout的組件。 Cardlayout具有作爲堆棧顯示頂層組件的功能。而且我們也可以手動配置要顯示的內容。如何在我的javaFX 2.0應用程序中實現awt.CardLayout的功能?
在javaFX 2.0中,有一個名爲StackPane的佈局。但它看起來不像Cardlayout。
在我的javaFX 2.0應用程序中,我需要替換使用awt.CardLayout的組件。 Cardlayout具有作爲堆棧顯示頂層組件的功能。而且我們也可以手動配置要顯示的內容。如何在我的javaFX 2.0應用程序中實現awt.CardLayout的功能?
在javaFX 2.0中,有一個名爲StackPane的佈局。但它看起來不像Cardlayout。
沒有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();
}
另一種選擇是使用StackPane
並設置所有的可見性,但CURENT Pane
到false
。不理想,但另一種思考問題的方式
我試着用TabPane。但它顯示一個標籤菜單。所以我採用了你在這裏建議的方式。它適合我的要求。感謝Sergey。 :) – Anuruddha
請注意,在2.2版本中有一個名爲'Pagination'的新控件,它可能更適合您的需求。 –