Hi,

I am writing a program to change the image of the button when it is clicked and image gets replaced to the original image when the button is clicked again. Have used array of button to do this. I have used integers whose value will continue to change as you keep clicking on these buttons and have been using if else statement in the program. but the issue is that when i click on the first button the image of all the buttons get's changed,which it seems is happening because the value of integer associated with each button get's changed simultaneously. But i have not been able to find any other way to solve this issue. Please advise any appropriate solution to the problem.

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=0,record1=0,record2=0,record3=0;
	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!=0)
			   {
				System.out.println("Value:" +record);
				System.out.println("Value of i="+i);
		     jButton[i].setIcon(new ImageIcon("D:/WorkSpace/cards/as.gif"));
				count++;
				}
               
               else{
			System.out.println("Value of record:="+record);
			jButton[i].setIcon(new ImageIcon("D:/WorkSpace/images.gif"));
                   //count--;
					
           	}
               record++;
            
             if(record1%2!=0)
			{
				
				System.out.println("Value of record1:" +record1);
				System.out.println("Value of i="+i);
			jButton[1].setIcon(new ImageIcon("D:/workSpace/cards/ac.gif"));
				//count++;
				//count--;
				}
			 else {
				 System.out.println("Value of record1::="+record1);
			 jButton[i].setIcon(new ImageIcon("D:/WorkSpace/images.gif"));
			     //count--;
			 }
		 	record1++;		 	
			
		 	if(record2%2!=0)
			{	
				
			  System.out.println("Value of record2:"+record2);
			  System.out.println("Value of i="+i); 
		      jButton[2].setIcon(new ImageIcon("D:/WorkSpace/cards/ad.gif"));
			    // count++;
			}
			 else {
			 jButton[i].setIcon(new ImageIcon("D:/WorkSpace/images.gif"));
			     //count--;
			 }
			record2++;
			
			if(record3%2!=0)
			{
				
                System.out.println("Value of record3:"+record3);
				System.out.println("Value of i="+i);
                jButton[3].setIcon(new ImageIcon("D:/workSpace/cards/ah.gif"));
				//count++;
			}
			else {
			jButton[i].setIcon(new ImageIcon("D:/WorkSpace/images.gif"));
                //count--;			
			}
			
			record3++;
				
				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

	}

	}

Can you post the console output for when you execute the code that shows the printouts?

if(record2%2!=0)

What is the purpose of that statement?

There is a lot of undocumented/unexplained logic in the actionPerformed method.
Can you explain what all that is about? There are many variables and tests that make no sense.

Edited 6 Years Ago by NormR1: n/a

This article has been dead for over six months. Start a new discussion instead.