Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By MR bruto

Thread: Trouble in getting the code to open a new frame on clicking a button

  1. #1
    Ashita is offline Member
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Red face Trouble in getting the code to open a new frame on clicking a button

    this is my code to make a phonebook...i want that it should open a new frame when clicked on "ADD" and another when clicked on "SEARCH".
    i m creating a code using actionlisteners but not getting it even after trying it for several times. Please anyone send the code to do the same.
    thanks!!!
    The code is:-
    import java.awt.*;
    class MyFrame
    {
    Frame f;
    Button b1,b2;
    Label l1;
    MyFrame()
    {
    f=new Frame("MyFrame");
    Panel p1=new Panel();
    Color c=new Color(141,28,255);
    p1.setBackground(c);



    l1=new Label("MY PHONEBOOK");
    b1=new Button("ADD");
    b2=new Button("SEARCH");
    l1.setBounds(95,50,300,50);
    b1.setBounds(50,100,180,20);
    b2.setBounds(50,145,180,20);

    Color c1=new Color(255,255,0);
    b1.setBackground(c1);
    Color c2=new Color(255,40,128);
    b2.setBackground(c2);
    p1.setLayout(null);
    p1.add(l1);
    p1.add(b1);
    p1.add(b2);
    f.add(p1);
    f.setSize(300,300);
    f.setVisible(true);
    }
    public static void main(String ar[])
    {
    MyFrame f=new MyFrame();
    }
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Trouble in getting the code to open a new frame on clicking a button

    Please wrap your code using the code tags.

    i m creating a code using actionlisteners but not getting it even after trying it for several times
    I see no ActionListeners within your code.

  3. #3
    Ashita is offline Member
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Default Re: Trouble in getting the code to open a new frame on clicking a button

    sir, this is just the starting of the phonebook. i was trying using actionlisteners but was not getting it.i m asking you the code( what to do after this code) to do the same mentioned in the thread.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Trouble in getting the code to open a new frame on clicking a button

    Quote Originally Posted by Ashita View Post
    sir, this is just the starting of the phonebook. i was trying using actionlisteners but was not getting it.i m asking you the code( what to do after this code) to do the same mentioned in the thread.
    I don't understand your question (or whether there even was a question). You're code shows no ActionListener implementation, but you say you've tried? You are much better of getting help by posting that code, with all the error messages, and asking a question. And please read the following if you have not already
    How to Write an Action Listener (The Java Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

  5. #5
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default Re: Trouble in getting the code to open a new frame on clicking a button

    how you use an actionlistener:

    you should add an actionlistener to the button first by doing this:

    Java Code:
    thehandler handler = new thehandler();
    b2.addActionListener(handler);
    Java Code:
    public class thehandler implements ActionListener{
    	public void actionPerformed(ActionEvent event){
    if(event.getSource()==b2){//<----the b2 is the button you want to make something do when clicked.
    (in here is is whatever you want to do.)
    }
    
    }
    }
    Last edited by MR bruto; 07-01-2013 at 05:51 PM.
    Ashita likes this.
    Never give up.

  6. #6
    Ashita is offline Member
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Default Re: Trouble in getting the code to open a new frame on clicking a button

    Sir,thanks a lot

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: Trouble in getting the code to open a new frame on clicking a button

    If you only add that ActionListener to button b2 there is no need to test whether or not the source of the event is b2 ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 2
    Last Post: 06-28-2013, 02:20 AM
  2. Replies: 6
    Last Post: 03-11-2012, 04:44 PM
  3. Replies: 6
    Last Post: 08-24-2011, 02:52 PM
  4. How to open new JFrame with clicking on button?
    By phearless in forum AWT / Swing
    Replies: 1
    Last Post: 01-14-2011, 10:36 AM
  5. Quit Button, Load new Frame, Timer Code
    By IHateNetbeans in forum New To Java
    Replies: 1
    Last Post: 03-18-2009, 03:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •