为什么我的动作监听器不工作? [英] Why aren't my actionlisteners working?

查看:111
本文介绍了为什么我的动作监听器不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在使用addActionListeners()方法添加动作侦听器时遇到麻烦,该方法位于一些system.out.printlns之间,以便让我知道该方法确实在起作用。

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;
    }
}

add actionlistener方法我尝试了许多不同的方法,例如使类实现一个actionListener,并使用 player1Cards [i] .addActionListener(this); ...这没有用,所以我改为:

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

这是当前的方式,找到了Java 8教程之后(我正在使用Java 8,这样可以吗?)
如果JButton不在集合中,并且大小相同,则所有玩家获得的卡数相同,从...开始。这是我的方法,无论哪个玩家先走,都应该调用它。但是它永远不会在控制台上打印一行...

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.

推荐答案

某些事情在您的代码中不太正确。

Some things are not quite right in your code.


  1. 您的for循环不正确:

  1. Your for loop is not correct:

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

必须是

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

您的for循环可以重写为:

Your for loop can be rewritten to:

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

因为显然, player1Cards 始终大于0,条件 i == player1Cards.length false

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.

您正在比较带有 == 从不这样做!始终使用 equals()比较字符串。那是因为对于对象引用, == 比较对象的 identity (内存位置)。对于字符串,是相同的。因此,值 player1的字符串与另一个具有相同值的字符串并不总是具有相同的标识。 equals()方法旨在比较要比较的对象的值。

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.

由Zabuza提示关于StackOverflow的答案详细说明了 == .equals()






您还应该避免重复变量,例如 player1Cards player2Cards 等。如果您扩展游戏并允许16名玩家怎么办?您必须复制粘贴很多东西。解决该问题的一种方法是为玩家使用数组,例如 playerCards [] 。另外,您应该阅读更多有关Java中面向对象的知识。它将指导您如何使用何时使用类和对象。


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.

这篇关于为什么我的动作监听器不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆