Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Please help with Actionlistener-Button

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy 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!

    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);
        }    
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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

Similar Threads

  1. How to Add ActionListener to a JButton in Swing?
    By JavaPF in forum Java Swing Tutorials
    Replies: 17
    Last Post: April 24th, 2013, 05:14 PM
  2. ActionListener help
    By QBird in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 1st, 2011, 12:25 PM
  3. need help with ActionListener,JPanel,JFrame
    By amahara in forum AWT / Java Swing
    Replies: 5
    Last Post: February 3rd, 2010, 01:40 PM
  4. Question about ActionListener
    By TimW in forum AWT / Java Swing
    Replies: 6
    Last Post: November 4th, 2009, 11:00 AM
  5. SOMEONE PLEASE HELP ME ADD THE ACTIONLISTENER INTERFACE...
    By beginning2Understand in forum AWT / Java Swing
    Replies: 5
    Last Post: June 30th, 2009, 12:42 AM

Tags for this Thread