Hi,
I am building a game in which i have used array for applying 52 button, which works fine.But when i try to use Action Listener with the array of button and try use setEnable(false)or true it does not work.I am also sending my.
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.*;
public class tasking extends JFrame implements ActionListener{
JMenuBar mb;
JMenu file,help;
JMenuItem newgame,exit,about,rules;
JButton b1[];
JPanel p1,p2;
int z,i,a,count=1;
String text;
ImageIcon as[];
JLabel score,time;
boolean bl,bl2;
public tasking(){
mb=new JMenuBar();
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();
ImageIcon as[]=new ImageIcon[52];
JButton b1[]=new JButton[52];
p1.setLayout(new GridLayout(4,2,5,5));
for(int i=0;i<b1.length;i++){
as[i]=new ImageIcon("D:/WorkSpace/images.gif");
b1[i]=new JButton(as[i]);
b1[i].addActionListener(this);
b1[i].setEnabled(false);
p1.add(b1[i]);
}
p2=new JPanel();
p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
score=new JLabel("Your Score: ");
time=new JLabel("Timer: ");
p2.add(score);
p2.add(time);
Container c1=getContentPane();
c1.setLayout(new BorderLayout());
c1.add(p1,BorderLayout.CENTER);
c1.add(p2,BorderLayout.SOUTH);
}
/**
* This method initializes this
*
*/
public void actionPerformed(ActionEvent e){
if(e.getSource().equals(exit)){
System.exit(0);
}
if(e.getSource().equals(newgame)){
try{
JButton b1[]=new JButton[52];
b1[i]=new JButton();
b1[i].setEnabled(true);
bl=b1[i].isEnabled();
System.out.println("Status:"+bl);
newgame.setEnabled(false);
}catch(Exception ex){
System.err.println("Exception occurred :" + ex.toString());
ex.printStackTrace();
}
Thread t=new Thread(){
public void run(){
for(a=1; a<=30; a++)
{
time.setText("Timer: "+ count);
count++;
if(a==30){
count=1;
newgame.setEnabled(true);
JButton b1[]=new JButton[52];
b1[i]=new JButton();
b1[i].setEnabled(false);
}
try{
repaint();
Thread.sleep(500);
}
catch(InterruptedException e)
{}
}
}
} ;
t.start();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
tasking ta=new tasking();
ta.setTitle("Multithreding");
ta.setSize(700,700);
ta.setVisible(true);
}
}