Please help with Actionlistener-Button
Please help me by fixing the actionlistener to the buttons. I want to display in textbox 1 when I press button 1, had no idea how to do that... please help!
Code :
import java.awt.*;
import java.awt.event.*;
public class MyPhone extends Frame{
public static void main(String arg[]){
new MyPhone();
}
public MyPhone(){
super ("My Phone");
setLayout(new BorderLayout());
TextField text1=new TextField (10);
add("Center",text1);
Button btn1=new Button("OK");
Button btn2=new Button("CANCEL");
Button btn3=new Button("CALL");
Button btn4=new Button("OFF");
Button btn5=new Button("UP");
Button btn6=new Button("DOWN");
Button btn7=new Button("LEFT");
Button btn8=new Button("RIGHT");
Button btn9=new Button(".");
Button btn10=new Button("7");
Button btn11=new Button("8");
Button btn12=new Button("9");
Button btn13=new Button("4");
Button btn14=new Button("5");
Button btn15=new Button("6");
Button btn16=new Button("1");
Button btn17=new Button("2");
Button btn18=new Button("3");
Button btn19=new Button("*");
Button btn20=new Button("0");
Button btn21=new Button("#");
Panel B1=new Panel();
B1.setLayout(new GridLayout(1,2));
B1.add(btn1);
B1.add(btn2);
Panel A1=new Panel();
A1.setLayout(new GridLayout(2,1));
A1.add(text1);
A1.add(B1);
Panel B2=new Panel();
B2.setLayout(new BorderLayout());
B2.add("North",btn5);
B2.add("South",btn6);
B2.add("West",btn7);
B2.add("East",btn8);
B2.add("Center",btn9);
Panel A2=new Panel();
A2.setLayout(new BorderLayout());
A2.add("West",btn3);
A2.add("East",btn4);
A2.add("Center",B2);
Panel A3=new Panel();
A3.setLayout(new GridLayout(4,3));
A3.add(btn10);
A3.add(btn11);
A3.add(btn12);
A3.add(btn13);
A3.add(btn14);
A3.add(btn15);
A3.add(btn16);
A3.add(btn17);
A3.add(btn18);
A3.add(btn19);
A3.add(btn20);
A3.add(btn21);
add("North",A1);
add("Center",A2);
add("South",A3);
resize(250,400);
show();
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
btn8.addActionListener(this);
btn9.addActionListener(this);
btn10.addActionListener(this);
btn11.addActionListener(this);
btn12.addActionListener(this);
btn13.addActionListener(this);
btn14.addActionListener(this);
btn15.addActionListener(this);
btn16.addActionListener(this);
btn17.addActionListener(this);
btn18.addActionListener(this);
btn19.addActionListener(this);
btn20.addActionListener(this);
btn21.addActionListener(this);
}
}
Re: Please help with Actionlistener-Button
You need to implement the ActionListener interface, and deal with each action accordingly. See How to Write an Action Listener for more information