import java.awt.*; import java.awt.event.*; class Bt implements ActionListener{ Frame f; Button b[]=new Button[3]; Button b1[]=new Button[3]; Button b2[]=new Button[3]; TextField tf; Bt(String s){ Frame f=new Frame(s); TextField tf=new TextField(); for(int i=0;i<b.length;i++){ b[i]=new Button(); b[i].setLabel(String.valueOf(i)); b[i].setBounds((60*i),250,30,30); //b[i].addActionListener(this); f.add(b[i]); } //for(int d=0;d<b.length;d++){ //b[d].addActionListener(this); //} tf.setBounds(20,60,100,100); f.add(tf); f.setLayout(null); f.setSize(400,400); f.setVisible(true); } public void actionPerformed(ActionEvent k){ /*for(int i=0;i<b.length;i++){ if(k.getSource()==b[i]){ switch(i){ case 0:tf.setText("Welcome"); //how can i check which button is generated event. case 1:tf.setText("Let's play"); case 2:tf.setText("Quit"); //break; } } }*/ } public static void main(String... arg){ new Bt("ButtonType"); } }
how can i check which button is generated event? I want if Button 1 is pressed then it show Welcome in texfield, if button 2 then it show let's play. how can i achieve it?