简体   繁体   English

为什么我的动作监听器不工作?

[英]Why aren't my actionlisteners working?

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. 因此,我在使用addActionListeners()方法(位于某些system.out.printlns之间)添加动作侦听器时遇到了麻烦,因此我无法确定该方法确实在工作。

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); 我尝试了多种不同的add actionlistener方法,例如使类实现actionListener,并使用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. 找到Java 8教程后,当前情况就是这样(我正在使用Java 8,所以应该没问题吗?)如果不是很明显,则JButton位于集合中,并且所有参与者都得到相同数量的JButton卡开始。 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. 提前致谢。

Some things are not quite right in your code. 有些事情在您的代码中不太正确。

  1. Your for loop is not correct: 您的for循环不正确:

     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: 您的for循环可以重写为:

     { 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. 因为显然, player1Cards的长度始终大于0,所以条件i == player1Cards.length在第一个循环中为false ,从而导致for循环立即中止。

  2. You are comparing strings with == . 您正在将字符串与==进行比较。 Never do that! 绝对不要那样做! Always use equals() to compare strings. 始终使用equals()比较字符串。 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. 这就是为什么值“ player1”的字符串与另一个具有相同值的字符串并不总是具有相同的标识。 The equals() method is designed to compare the values of the objects being compared. equals()方法旨在比较要比较的对象的值。

    As hinted by Zabuza , this answer on StackOverflow explains more about what's the difference between == and .equals() . 正如Zabuza暗示的有关StackOverflow的答案进一步说明了==.equals()之间的区别。


You should also avoid variable repetion like player1Cards , player2Cards et cetera. 您还应避免重复变量,例如player1Cardsplayer2Cards等。 What if you extend the game and allow 16 players? 如果您扩展游戏并允许16名玩家怎么办? 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[] . 解决该问题的一种方法是为玩家使用数组,例如playerCards[] Also, you should read up a little bit more about object-orientation in Java. 另外,您应该阅读更多有关Java中面向对象的知识。 It'll guide you how en when to use classes and objects. 它将指导您何时使用类和对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM