Hi,

I have written a program where i have used array of 4 Buttons as i click on any of the buttons it image changes and on re-clicking the same button it images changes back to the original image.It is working alright but the issue is that the image which appears on clicking any of the buttons is the image which i have used in the forth button and images used by me for other buttons do not appear.

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import java.util.*;

public class arraytest1 implements ActionListener {

	JButton jButton[],b2;
	int count=0,record,record1,record2,record3;
	int i,z;
	String tl;
	JPanel p1;
	Object ia[];
	String gifns[];
	JMenuBar mb;
	JMenu file,help;
	JMenuItem newgame,exit,about,rules;
	
	public arraytest1(){
		
		JFrame jframe = new JFrame();
		
		mb=new JMenuBar();
		jframe.setJMenuBar(mb);
		file=new JMenu("File");
		newgame=new JMenuItem("New Game");
		exit=new JMenuItem("Exit");
		mb.add(file);
		file.add(newgame);
		file.add(exit);
		newgame.addActionListener(this);
		exit.addActionListener(this);
		help=new JMenu("Help");
		about=new JMenuItem("About");
		rules=new JMenuItem("Game Rules");
		mb.add(help);
		help.add(rules);
		help.add(about);
		
		p1=new JPanel();
		jButton=new JButton[4];
		p1.setLayout(new GridLayout(4,2,5,5));
		for(int i=0;i<jButton.length;i++){
jButton[i] =new JButton(Integer.toString(i+1),new ImageIcon("D:/WorkSpace/images.gif"));
		jButton[i].addActionListener(this);
		jButton[i].addActionListener(new NewListener());
		p1.add(jButton[i]);
		System.out.println("i:"+i);
		p1.setVisible(true);
		}
		  jframe.add(p1);
		  jframe.pack();
	      jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	      jframe.setVisible(true);
	      record++;
	      record1++;
	      record2++;
	      record3++;
	      	      
	}
		
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource().equals(exit)){
			System.exit(0);
		}
		
		if(e.getSource().equals(newgame)){
			try{
				//newgame.setEnabled(false);
				p1.setVisible(false);
				
			}catch(Exception ex){
			System.err.println("Exception occurred :" + ex.toString());
				ex.printStackTrace();
			}
			
			}
	}
	private class NewListener implements ActionListener
	
	{
		/* (non-Javadoc)
		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
		 */
		
		
		public void actionPerformed(ActionEvent e){
						
			try{
				String tl=((JButton)e.getSource()).getText();
				System.out.println("Value of text " + tl);
				i=Integer.parseInt(tl);
                i--;
				
               if(record%2==1)
			   {
				System.out.println("Value:" +record);
			jButton[i].setIcon(new ImageIcon("D:/WorkSpace/cards/as.gif"));
				count++;
				
				}
				else {
			jButton[i].setIcon(new ImageIcon("D:/WorkSpace/images.gif"));
                   count--;
                   
				}
							
			if(record1%2==1)
			{
				System.out.println("Value:" +record1);
			jButton[i].setIcon(new ImageIcon("D:/workSpace/cards/ac.gif"));
				count++;
				
				}
			 else{
			 jButton[i].setIcon(new ImageIcon("D:/WorkSpace/images.gif"));
			     count--;
			 }
		 				
			if(record2%2==1)
			{		
			  System.out.println("Value:"+record2);
		   jButton[i].setIcon(new ImageIcon("D:/WorkSpace/cards/ad.gif"));
			     count++;
			}
			 else {
			 jButton[i].setIcon(new ImageIcon("D:/WorkSpace/images.gif"));
			     count--;
			 }
						
			if(record3%2==1)
			{
                System.out.println("Value:"+record3);
			jButton[i].setIcon(new ImageIcon("D:/workSpace/cards/ah.gif"));
				count++;
			}
			else {
			jButton[i].setIcon(new ImageIcon("D:/WorkSpace/images.gif"));
                count--;			
			}
			
			record++;
			record1++;
			record2++;
			record3++;
				//i+=record;
				System.out.println("Score:="+count);
				
				if(count==4){
					System.out.println("Score:"+count);
				}
									 
			}catch(Exception ex){
			System.err.println("Exception occurred :"+ex.toString());
					ex.printStackTrace();
				}
			
			 	}
			
	}
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		
		new arraytest1();
		// TODO Auto-generated method stub

	}

	}

Sounds like an indexing problem.
Try debugging your code by printing out the values of the indexes that you are using as they are set and changed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.