Why aren't my actionlisteners working?

a 夏天 提交于 2019-12-25 09:29:04

问题


So I am having trouble adding actionlisteners using the method addActionListeners(), which is between some system.out.printlns so that I could tell that the method was actually working.

protected void whoFirst(String first) {
    int currPlayer = 0;
    System.out.println("Hello");
    addActionListeners();
    System.out.println("How are you?");
    if(first == "player1") {
        player1.setVisible(true);
        currPlayer = 1;
    }
    if(first == "player2") {
        player2.setVisible(true);
        currPlayer = 2;
    }
}

The add actionlistener method I have tried many different ways such as making the class implement an actionListener, and using player1Cards[i].addActionListener(this);... This didn't work so I changed to this:

private void addActionListeners() {
            System.out.println("Number of players = : " + players );
            for(int i = 0; i == player1Cards.length ; i++) {
            if(players == 2) {
                player1Cards[i].addActionListener(e -> cardActions());
                player2Cards[i].addActionListener(e -> cardActions());
            }
            if(players == 3) {
                player1Cards[i].addActionListener(e -> cardActions());
                player2Cards[i].addActionListener(e -> cardActions());
                player3Cards[i].addActionListener(e -> cardActions());
            }
            if(players == 4) {
                player1Cards[i].addActionListener(e -> cardActions());
                player2Cards[i].addActionListener(e -> cardActions());
                player3Cards[i].addActionListener(e -> cardActions());
                player4Cards[i].addActionListener(e -> cardActions());
            }
        }
    }

This is how it is currently, after finding a java 8 tutorial (I am using java 8, so should be fine?) If its not obvious the JButtons are in a collection and all the same size as all the players get the same amount of cards to start with. This is my method which is supposed to call no matter which player goes first... But it never prints a line to the console...

private void cardActions() {
    System.out.println("Whats up?");
}

I feel like this should have worked in either of the cases but if anyone has any suggestions that would help that would be fantastic. Thanks in advance.


回答1:


Some things are not quite right in your code.

  1. Your for loop is not correct:

    for (int i = 0; i == player1Cards.length; i++)
    

    must be

    for (int i = 0; i < player1Cards.length; i++)
    

    Your for loop can be rewritten to:

    {
        int i = 0;
        while (i == player1Cards.length) {
            // code inside for loop
            i++;
        }
    }
    

    Because apparently, the length of player1Cards is always greater than 0, the condition i == player1Cards.length is false at the first loop, causing the for loop to immediately abort.

  2. You are comparing strings with ==. Never do that! Always use equals() to compare strings. That is because for object references, == compares the identity (memory location) of the objects. For strings, it's the same. That's why a string with value "player1" has not always the same identity as another string with the same value. The equals() method is designed to compare the values of the objects being compared.

    As hinted by Zabuza, this answer on StackOverflow explains more about what's the difference between == and .equals().


You should also avoid variable repetion like player1Cards, player2Cards et cetera. What if you extend the game and allow 16 players? You have to copy-paste a lot of things. One way to address that problem is to use an array for the players, for example playerCards[]. Also, you should read up a little bit more about object-orientation in Java. It'll guide you how en when to use classes and objects.



来源:https://stackoverflow.com/questions/45107681/why-arent-my-actionlisteners-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!