英文:
Button's icon not appear when click on it
问题
package ButtonsGsme;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Buttons extends JFrame implements ActionListener {
    JButton lastPressed = null;
    int buttonsPressed = 0;
    Cards c = new Cards();
    Icon[] iconPaths = {new ImageIcon(getClass().getResource("tiger.png")),
            new ImageIcon(getClass().getResource("tiger.png")),
            new ImageIcon(getClass().getResource("chicken.png")),
            new ImageIcon(getClass().getResource("chicken.png"))};
    ArrayList<Icon> icons = new ArrayList<>();
    JButton buttonsArr[] = new JButton[4];
    public Buttons() {
        setLayout(null);
        for (int i = 0; i < 4; i++) {
            buttonsArr[i] = new JButton();
        }
        for (int i = 0; i < 4; i++) {
            icons.add(iconPaths[i]);
        }
        Collections.shuffle(icons);
        buttonsArr[0].setBounds(100, 100, 200, 140);
        buttonsArr[1].setBounds(350, 100, 200, 140);
        buttonsArr[2].setBounds(100, 300, 200, 140);
        buttonsArr[3].setBounds(350, 300, 200, 140);
        for (int i = 0; i < 4; i++) {
            add(buttonsArr[i]);
            buttonsArr[i].addActionListener(this);
        }
    }
    public void pressedIcon(JButton b) {
        for (int i = 0; i < 4; i++) {
            if (b == buttonsArr[i]) {
                buttonsArr[i].setIcon(icons.get(i));
            }
        }
    }
    public boolean matched(JButton b1, JButton b2) {
        if (b1.getIcon().toString().equals(b2.getIcon().toString())) {
            return true;
        } else {
            return false;
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        buttonsPressed++;
        pressedIcon((JButton) e.getSource());
        if (buttonsPressed == 2) {
            try {
                // To stop a second with two buttons' icons show face up
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException ex) {
                Logger.getLogger(Buttons.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (matched(lastPressed, (JButton) e.getSource()) == false) {
                lastPressed.setIcon(null);
                ((JButton) e.getSource()).setIcon(null);
            }
            buttonsPressed = 0;
        } else {
            lastPressed = (JButton) e.getSource();
        }
    }
}
Note: The above code is a direct translation of the provided Java code into Chinese. If you have any questions or need further assistance, feel free to ask.
英文:
I am making a memory icons game in a very simple way. I have a problem when I clicked on the second button the icon of that but not appear but the code can do everything else correct, i think the problem is in the actionPerformed method where i deal with the clicked buttons:
package ButtonsGsme;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Buttons extends JFrame implements ActionListener{
    JButton lastPressed = null;
    int buttonsPressed = 0;
    Cards c = new Cards();
     Icon[] iconPaths = {new ImageIcon(getClass().getResource("tiger.png")),
                     new ImageIcon(getClass().getResource("tiger.png")),
                     new ImageIcon(getClass().getResource("chicken.png")),
                     new ImageIcon(getClass().getResource("chicken.png"))};
    ArrayList<Icon> icons = new ArrayList <>();
    JButton buttonsArr[] = new JButton[4];
    public Buttons(){
        setLayout(null);
        for(int i = 0; i < 4; i++) {
            buttonsArr[i] = new JButton();
        }
        for(int i = 0; i < 4; i++) {
            icons.add(iconPaths[i]);
        }
        Collections.shuffle(icons);
        
        buttonsArr[0].setBounds(100, 100, 200, 140);
        buttonsArr[1].setBounds(350, 100, 200, 140);
        buttonsArr[2].setBounds(100, 300, 200, 140);
        buttonsArr[3].setBounds(350, 300, 200, 140);
        
        for (int i = 0; i < 4; i++) {
            add(buttonsArr[i]);
            buttonsArr[i].addActionListener(this);
        }
    }
    // To give each button its icon
    public void pressedIcon(JButton b) {
        for (int i = 0; i < 4; i++){
            if(b == buttonsArr[i]) {
                buttonsArr[i].setIcon(icons.get(i));
            }
        }
    }
    public boolean matched(JButton b1, JButton b2){
        if (b1.getIcon().toString().equals(b2.getIcon().toString())) {
            return true;
        }
        else {
            return false;
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        buttonsPressed++;
        pressedIcon((JButton) e.getSource());
        if (buttonsPressed == 2) {
            try {
                // To stop a second with two buttons' icons show face up
                TimeUnit.SECONDS.sleep(1);
            } 
            catch (InterruptedException ex) {
                Logger.getLogger(Buttons.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (matched(lastPressed, (JButton) e.getSource()) == false) {
                lastPressed.setIcon(null);
                ((JButton) e.getSource()).setIcon(null);
            }
            buttonsPressed = 0;
        }
        else {
            lastPressed = (JButton) e.getSource();
        }
    }
} 
This is what happen when i click on the second button (the bottom left) it holds like that for a second (the timer that i set) then nothing appear
专注分享java语言的经验与见解,让所有开发者获益!


评论