【问题标题】:How can I implement the functionality of awt.CardLayout in my javaFX 2.0 application?如何在我的 javaFX 2.0 应用程序中实现 awt.CardLayout 的功能?
【发布时间】:2011-11-29 11:14:36
【问题描述】:

在我的 javaFX 2.0 应用程序中,我需要替换一个使用 awt.CardLayout 的组件。 Cardlayout 具有作为堆栈的功能,可显示堆栈中的顶部组件。我们也可以手动配置要显示的内容。

在 javaFX 2.0 中,有一个名为 StackPane 的布局。但它看起来不像 Cardlayout。

【问题讨论】:

    标签: javafx-2


    【解决方案1】:

    没有 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();
    
    }
    

    【讨论】:

    • 我尝试使用 TabPane。但它显示一个选项卡菜单。所以我使用了你在这里建议的方式。它可以很好地满足我的要求。谢谢谢尔盖。 :)
    • 注意,在 2.2 版本中有一个名为 Pagination 的新控件可能会更好地满足您的需求。
    • 这个答案可能不是最好的,因为它会强制重新绘制您的Group。这意味着如果您的Group 包含大量信息,则会在切换时引入明显的延迟。
    【解决方案2】:

    另一个选项是使用StackPane 并将除当前Pane 之外的所有可见性设置为false。不理想,但换一种思路来考虑问题

    【讨论】:

    • 其实你的答案比上面的答案好。这可以防止重新绘制窗格。我有一个窗格,上面有大约 12 个选项卡,使用这个问题的解决方案,切换到它大约需要 200 毫秒。使用您的答案,它变得即时。
    • 这是我认为最好的答案。
    【解决方案3】:

    使用 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);
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-13
      • 2010-09-19
      • 2023-04-05
      • 2011-12-13
      • 1970-01-01
      • 1970-01-01
      • 2014-05-14
      • 1970-01-01
      相关资源
      最近更新 更多