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.