【问题标题】:Unknown number of buttons, how to add a different Event Action to each JavaFX未知数量的按钮,如何为每个 JavaFX 添加不同的事件操作
【发布时间】:2020-03-11 16:27:44
【问题描述】:

我有一个 JavaFX GUI,它根据团队的数量创建和添加许多按钮,这些按钮存储在一个 ArrayList 中,这个数字会有所不同。

每个团队都有一份人员名单。

我的问题是我想编写某种逻辑,在不知道我可以添加按钮列表的情况下添加按钮列表,然后单击每个按钮将显示其不同的花名册。

        Button getTeamsButton = new Button( "Load Teams and Details" );
        getTeamsButton.setOnAction( e -> {
        getTeams.scrapeTeams();
            if( !getTeams.getTeams().isEmpty() )                      
            {             

                for( int i = 0; i < getTeams.getTeams().size(); i++ )
                {
                            Button teamName = new Button( getTeams.getTeams().get(i).getTeamName()  );
        teamNamesButtons.add( teamName );
        layout1.getChildren().add( teamNamesButtons.get( i ) );

                }

我能够创建按钮。

                getTeamsButton.setDisable( true );
                teamNamesButtons.get( 0 ).setOnAction( f -> {
                    for(int i = 0; i < getTeams.getTeams().get( 0 ).getRoster().size(); i++) {
                        System.out.println(getTeams.getTeams().get( 0 ).getRoster().get( i ));
                    }
                });                    
            }
        } );

如果我指定列表中的哪个项目可以打印名册,我如何解释顶部按钮的逻辑是团队 0,第二个是团队 1,等等?

任何建议将不胜感激?

【问题讨论】:

    标签: java button javafx arraylist


    【解决方案1】:

    您的要求并不完全清楚,但也许这会有所帮助。

    您在创建按钮时已经“知道”它具有哪个索引:索引由i 给出。因此,您只需创建一个引用该值的事件处理程序。这里唯一的问题是,您不能在 lambda 表达式中引用非最终变量(或不能通过简单地添加 final 关键字而成为最终变量的变量),因此您可能需要将其复制到最终变量。

    例如:

    Button getTeamsButton = new Button( "Load Teams and Details" );
    getTeamsButton.setOnAction( e -> {
        getTeams.scrapeTeams();
        if( !getTeams.getTeams().isEmpty() ) {             
            for( int i = 0; i < getTeams.getTeams().size(); i++ ) {
                Button teamName = new Button( 
                     getTeams.getTeams().get(i).getTeamName()  );
                teamNamesButtons.add( teamName );
                layout1.getChildren().add( teamNamesButtons.get( i ) );
    
                final int index = i ;
                teamName.setOnAction(evt -> {
                    for (int j = 0 ; j < getTeams.getTeams().get(index).getRoster().size(); j++) {
                        System.out.println(getTeams.getTeams().get(index).getRoster().get( j ));
                    }
                });
            }
        }
    });
    

    不过,您可能并不真的需要索引;只是相关数据。您还没有显示列表中的实际内容,但假设您已经定义了某种 Team 类,您可以使用

    Button getTeamsButton = new Button( "Load Teams and Details" );
    getTeamsButton.setOnAction( e -> {
        getTeams.scrapeTeams();
        // the if statement here is redundant, if the list is
        // empty, the for loop will just iterate zero times....     
        for( int i = 0; i < getTeams.getTeams().size(); i++ ) {
            Team team = getTeams.getTeams().get(i);
            Button teamName = new Button(team.getTeamName());
            teamNamesButtons.add(teamName);
            layout1.getChildren().add(teamName);
    
            teamName.setOnAction(evt -> {
                for (int j = 0 ; j < team.getRoster().size(); j++) {
                    System.out.println(team.getRoster().get( j ));
                }
            });
        }
    });
    

    或者,使用首选语法

    Button getTeamsButton = new Button( "Load Teams and Details" );
    getTeamsButton.setOnAction( e -> {
        getTeams.scrapeTeams();
        for( Team team : getTeams.getTeams()) {
             Button teamName = new Button(team.getTeamName());
             teamNamesButtons.add(teamName);
             layout1.getChildren().add(teamName);
    
             teamName.setOnAction(evt -> {
                 for (Player player : team.getRoster) {
                      System.out.println(player);
                 }
             });
         }
    });
    

    (您也不应该真的需要 teamNamesButtons 列表...请注意上面的代码中不再需要它。)

    【讨论】:

    • 您先生是个天才,非常感谢。我什至无法很好地解释自己,但你回答得很好,你的最终解决方案更加整洁。非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 2018-02-18
    • 1970-01-01
    • 2020-12-29
    • 2017-01-03
    • 1970-01-01
    相关资源
    最近更新 更多