How can I implement the functionality of awt.CardL

2019-01-25 05:52发布

In my javaFX 2.0 app, I need to replace a component which is used awt.CardLayout. Cardlayout has a functionality as a stack which displays the top component in stack. And also we can manually configure which is to be displayed.

In javaFX 2.0, there is a layout called StackPane. But It doesn't seems like Cardlayout.

标签: javafx-2
3条回答
ゆ 、 Hurt°
2楼-- · 2019-01-25 06:34

Another option is to use a StackPane and set the visibility of all but the curent Pane to false. Not ideal, but another way of thinking about the problem

查看更多
爷的心禁止访问
3楼-- · 2019-01-25 06:38

There is no CardLayout, but you can use TabPane or simply switch groups:

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

}
查看更多
不美不萌又怎样
4楼-- · 2019-01-25 06:43

Using metasim's answer, here is the full code (also made the buttons act more like toggle buttons):

public void start(Stage stage)
{

    VBox vbox = new VBox(5);

    RadioButton btn = new Button("1");
    RadioButton btn2 = new Button("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);
        }
    }

}
查看更多
登录 后发表回答