删除switch语句中的编辑

时间:2019-05-05 02:18:24

标签: java switch-statement

我正在创建一个扑克赔率计算器,我在一个带有JComboBox的桌子周围放置了8位玩家,其中包含选定数量的玩家。我要做的是在做出多项选择之后“重新绘制” GUI。例如,如果我单击4个播放器,则4个播放器变为可编辑状态。当前可以使用,但是当我选择3个播放器后,播放器4仍可编辑。

我尝试设置可见性,我厌倦了删除播放器窗格,我尝试了remove(addPlayer4),我尝试了其他一些奇怪的事情,但是我能得到的最接近的方法是完全删除播放器。

addPlayer1.addActionListener((ActionEvent e) -> {
        player1.removeAll();
        player1.add(playerPanelFactory("Player1",1));
        //welcomeScreen.get
        repaint();
        validate();
    });
    addPlayer2.addActionListener((ActionEvent e) -> {
        player2.removeAll();
        player2.add(playerPanelFactory("Player2",2));
        getParent().repaint();
 //       contentPane.repaint();
        validate();
    });
    addPlayer3.addActionListener((ActionEvent e) -> {
        player3.removeAll();
        player3.add(playerPanelFactory("Player3",3));
        getParent().repaint();
//        contentPane.repaint();
        validate();
    });
    addPlayer4.addActionListener((ActionEvent e) -> {
        player4.removeAll();
        player4.add(playerPanelFactory("Player4",4));
        repaint();
        validate();
    });

.....

playerBox.addActionListener((ActionEvent e)-> {
        numberOfPlayers = playerBox.getSelectedItem().toString();
        int numPlayers = Integer.parseInt(numberOfPlayers);
        switch(numPlayers){
            case 1: 
                addPlayer1.doClick();
                break;
            case 2:
                addPlayer1.doClick();
                addPlayer2.doClick();
                break;
            case 3:
                addPlayer1.doClick();
                addPlayer2.doClick();
                addPlayer3.doClick();
                break;
            case 4:
                addPlayer1.doClick();
                addPlayer2.doClick();
                addPlayer3.doClick();
                addPlayer4.doClick();
                break;
            case 5:
                addPlayer1.doClick();
                addPlayer2.doClick();
                addPlayer3.doClick();
                addPlayer4.doClick();
                addPlayer5.doClick();
                break;
            case 6:
                addPlayer1.doClick();
                addPlayer2.doClick();
                addPlayer3.doClick();
                addPlayer4.doClick();
                addPlayer5.doClick();
                addPlayer6.doClick();
                break;
            case 7:
                addPlayer1.doClick();
                addPlayer2.doClick();
                addPlayer3.doClick();
                addPlayer4.doClick();
                addPlayer5.doClick();
                addPlayer6.doClick();
                addPlayer7.doClick();
                break;
            case 8:
                addPlayer1.doClick();
                addPlayer2.doClick();
                addPlayer3.doClick();
                addPlayer4.doClick();
                addPlayer5.doClick();
                addPlayer6.doClick();
                addPlayer7.doClick();
                addPlayer8.doClick();

                break;
            default:
                return;
        }
    });

我不想链接所有代码,因为它几乎有700行,但是我认为这可能是问题所在。我希望获得的目标是让玩家窗格在被选中时可编辑,而在未被选中时则不可编辑。

https://imgur.com/a/ssgXSXW? 我希望这是有道理的

1 个答案:

答案 0 :(得分:0)

希望这种调整将有助于解决您的问题。本质上,每次更改计数器时,您都会添加一些玩家,每次更改时都重新添加他们,例如当选择1个玩家时,您将生成玩家1。然后,当选择两个玩家时,您将再次生成玩家1和玩家2。也许添加某种形式的支票,这样您就不会再次创建相同的玩家了。很好,那么您不必每次都添加玩家就清除它们,尽管只是一个小小的想法。

以此建议为目标,基本上是将当前玩家人数与之前的玩家人数进行比较。如果我们要添加更多玩家或保持不变,请照常继续操作并更新变量。但是,如果我们要删除一个或多个播放器,请在重新绘制之前删除当前播放器数量和目标播放器数量之间的播放器。

示例案例:从7位玩家增加到5位玩家。因此,我们要删除新的玩家(5)+ 1,即玩家6。然后循环并删除最后一个玩家,即玩家7。然后重画。

如果没有代码,我将无法对其进行测试,但是我希望我的头脑风暴对您有所帮助,或者希望它激发您自己或其他人的某些想法:)希望它也可以实现。如果有人知道这样做的更好方法,那么我也全神贯注,并渴望了解我可能在哪里出错了! :)

int lastNumPlayers;

/*
*   SOME CODE
*/

playerBox.addActionListener((ActionEvent e)-> {
        numberOfPlayers = playerBox.getSelectedItem().toString();
        int numPlayers = Integer.parseInt(numberOfPlayers);

        if(numPlayers >= lastNumPlayers) {
            switch(numPlayers){
                case 1: 
                    addPlayer1.doClick();
                    break;
                case 2:
                    addPlayer1.doClick();
                    addPlayer2.doClick();
                    break;
                case 3:
                    addPlayer1.doClick();
                    addPlayer2.doClick();
                    addPlayer3.doClick();
                    break;
                case 4:
                    addPlayer1.doClick();
                    addPlayer2.doClick();
                    addPlayer3.doClick();
                    addPlayer4.doClick();
                    break;
                case 5:
                    addPlayer1.doClick();
                    addPlayer2.doClick();
                    addPlayer3.doClick();
                    addPlayer4.doClick();
                    addPlayer5.doClick();
                    break;
                case 6:
                    addPlayer1.doClick();
                    addPlayer2.doClick();
                    addPlayer3.doClick();
                    addPlayer4.doClick();
                    addPlayer5.doClick();
                    addPlayer6.doClick();
                    break;
                case 7:
                    addPlayer1.doClick();
                    addPlayer2.doClick();
                    addPlayer3.doClick();
                    addPlayer4.doClick();
                    addPlayer5.doClick();
                    addPlayer6.doClick();
                    addPlayer7.doClick();
                    break;
                case 8:
                    addPlayer1.doClick();
                    addPlayer2.doClick();
                    addPlayer3.doClick();
                    addPlayer4.doClick();
                    addPlayer5.doClick();
                    addPlayer6.doClick();
                    addPlayer7.doClick();
                    addPlayer8.doClick();

                    break;
                default:
                    return;
        }

        lastNumPlayers = numPlayers;
    } else {        
        //Loop through all players between the last number and the new number
        for(int i = numPlayers + 1; i <= lastNumPlayers; i++) {
            switch(i) {
                //Lowest number of players is 1, so lowest check is at 2 (1+1 = 2)
                case 2:
                    player2.removeAll();
                    break;
                case 3: 
                    player3.removeAll();
                    break;
                case 4:
                    player4.removeAll();
                    break;
                case 5:
                    player5.removeAll();
                    break;
                case 6:
                    player6.removeAll();
                    break;
                case 7:
                    player7.removeAll();
                    break;
                case 8:
                    player8.removeAll();
            }
        }

        //After removing unused players, redraw screen
        repaint();
        lastNumPlayers = numPlayers;
    }

});