import java.awt.*;
import java.awt.event.*;
public class ArrayButton extends JFrame{
private String []str={"RED","BLUE","ORANGE","GREEN","BLACK"};
private Color []c={Color.RED,Color.BLUE,Color.ORANGE,Color.GREEN,Color.BLACK};
private JTextArea txt;
private Container canvas;
private Container canvas;
public ArrayButton(){
canvas=getContentPane();
canvas.setLayout(null);
txt=new JTextArea();
txt.setEditable(false);
txt.setBounds(40,10,100,25);
canvas.add(txt);
createArrayButton();
this.setBounds(100,100,180,230);
this.setVisible(true);
}
public void createArrayButton(){
JButton []btn=new JButton[5];
for(int i=0;i<btn.length;i++){
btn[i]=new JButton(str[i]);
btn[i].setBackground(c[i]);
btn[i].setForeground(Color.WHITE);
btn[i].setBounds(40,40+i*30,100,25);
final int index=i;
btn[i].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
update(index);
}
});
canvas.add(btn[i]);
}
}
public void update(int i){
txt.setText(str[i]);
canvas.setBackground(c[i]);
}
public static void main(String []args){
ArrayButton ab=new ArrayButton();
ab.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Sample Running :
Created By : Z-man, 2012
No comments:
Post a Comment