Javafx 2.0アプリケーションでAWT.CardLayoutの機能を実装するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/8309802

  •  25-10-2019
  •  | 
  •  

質問

Javafx 2.0アプリでは、awt.cardlayoutを使用するコンポーネントを置き換える必要があります。 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();

}

他のヒント

別のオプションは、aを使用することです StackPane そして、キュエント以外のすべての可視性を設定します Panefalse. 。理想的ではありませんが、問題についての別の考え方

Metasimの回答を使用して、完全なコードを次に示します(ボタンをトグルボタンのように動作させました):

public void start(Stage stage)
{

    VBox vbox = new VBox(5);

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

    ToggleGroup group = new ToggleGroup();
    btn.setToggleGroup(group);
    btn2.setToggleGroup(group);

    btn.getStyleClass().remove("radio-button");
    btn.getStyleClass().add("toggle-button");        


    btn2.getStyleClass().remove("radio-button");
    btn2.getStyleClass().add("toggle-button");        

    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"));

    cardsPane.getChildren().addAll(card1, card2);
    btn.setOnAction(new EventHandler<ActionEvent>()
    {
        public void handle(ActionEvent t)
        {
            showNodeHideNodes(cardsPane.getChildren(), card1);
        }
    });

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

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

}

private static void showNodeHideNodes(List<Node> nodes, Node nodeToShow)
{
    for (Node node : nodes)
    {
        if (node.equals(nodeToShow))
        {
            node.setVisible(true);
        } else
        {
            node.setVisible(false);
        }
    }

}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top