0
public class Concentration extends JFrame implements ActionListener{
    private JButton buttons[][]=new JButton[4][4];
    int i,j,n;
    public int open=0;
    private JButton  opens[]=new JButton[1];
    public ImageIcon  images[] = new ImageIcon[20];

    public Concentration() {

        super ("Concentration");    
        JFrame frame=new JFrame();
        setSize(1000,1000);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel=new JPanel(new GridLayout(4,4));
        panel.setSize(400, 400);

        for(i=0; i<16; i++){

             images[i]=new ImageIcon(getClass().getResource("/images/1 ("+i+").jpg"));

    }

    //shuffle

        for( i=0; i<buttons.length; i++){
            for (j=0; j<buttons[i].length;j++){ 
                n=i*buttons.length+buttons[i].length;
                buttons[i][j]=new JButton(images[i*buttons.length+j]);

                panel.add(buttons[i][j]);
                buttons[i][j].addActionListener(this);

            }
        }
        add(panel);
        pack();
        setVisible(true);

    }


    public void actionPerformed(ActionEvent e) {
        if(e.getSource() instanceof JButton){
            JButton pressedButton = (JButton) e.getSource();
            opens[open]=(JButton) e.getSource();
            if((pressedButton.getIcon() == null)){
                pressedButton.setIcon(new ImageIcon(getClass().getResource("/images/2.jpg")));
                open=open++;
            } else {   
                //pressedButton.setIcon(null);
            }

            }
        if (open==1){
            opens[0].setIcon(null);
            opens[1].setIcon(null);
        }
    }

    public static void main(String args[]){
        new Concentration();
    }
}

I want to a memory game. If two buttons are same, it will stay them open. But when i execute this, all images are open. WHwn click, they are still open.

Where is my wrong?

I used imageicon instead of images, does it matter?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
clara sampson
  • 11
  • 1
  • 7
  • I saw a [recommendation](http://stackoverflow.com/questions/2235569/add-and-remove-an-icon-on-a-jlabel) to have `revalidate()` after setting `Icon` to `null`. – PM 77-1 Sep 23 '13 at 17:31
  • sorry... but i have no idea what you are saying / want ?. if your english isn't good then use some proper translator so we can understand you – Maciej Cygan Sep 23 '13 at 18:30

0 Answers0