Results 1 to 6 of 6
  1. #1
    emily.m is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Have problem with add.ActionListener()

    Hi
    I want to add ActionListener for this button and make it react. But writing this line of code in go() method give me an error. Can someone do it for me please? (line 33)
    thanks

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.JPanel;
    import java.awt.event.*;
    
    
     public class GrowCircleTest implements ActionListener
    {
        public static final long serialVersionUID = 1L;
        public static int diameter = 50;
        JButton button;
    
        public static class CircleDrawPanel extends JPanel
        {
            public void paintComponent(Graphics g)
            {
                int x = 225;
                int y = 200;
    
                g.setColor(Color.BLUE);           //blue oval
                g.fillOval(x,y,diameter,diameter);//fill ovel
            }
    
            public void go()
            {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
                JButton button = new JButton("click me to grow the circle");   //c
                //button.addActionListener(this);
                //button.addActionListener(new increaseCircle());
    
                CircleDrawPanel myPanel = new CircleDrawPanel();
    
    
                frame.getContentPane().add(BorderLayout.EAST, button);      //c
    
                frame.getContentPane().add(myPanel);
                frame.setSize(500,500);//display 500*500 JFrame
                frame.setVisible(true);
            }
        }
        public static void main(String[]args)
            {
                CircleDrawPanel myPanel = new CircleDrawPanel();
                myPanel.go();
            }
    
           //public static class increaseCircle implements ActionListener
           // {
                public void actionPerformed(ActionEvent event)
                {
                    button.setText("haha :)");
                }
           // }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Have problem with add.ActionListener()

    writing this line of code in go() method give me an error.
    How about posting the specific details of the error? Most of us here don't like playing guessing games.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Have problem with add.ActionListener()

    this is an instance of CircleDrawPanel, and not of GrowCircleTest (because its a static class!) so its not implementing the listener interface. you could move the implements ActionListener (and the actionPerformed method) into the CircleDrawPanel class!

  4. #4
    emily.m is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Re: Have problem with add.ActionListener()

    How about posting the specific details of the error? Most of us here don't like playing guessing games.
    How about line 33 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  5. #5
    emily.m is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Re: Have problem with add.ActionListener()

    How can I add implements ActionListener to CircleDrawPanel extends JPanel ?
    Because CircleDrawPanel have extends JPanel.
    Please tell me how can I do this?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Have problem with add.ActionListener()

    Quote Originally Posted by emily.m View Post
    How about line 33 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    Nice, it's not the complete error message. That may be a bit more useful than a bunch of exclamation points.

    But regardless, don't have your GUI class implement ActionListener because that's asking it to do too much. I strongly advise you to use either anonymous inner ActionListener class, or a private inner ActionListener class, or a stand alone class.

Similar Threads

  1. Problem with ActionListener?
    By Manish87 in forum AWT / Swing
    Replies: 5
    Last Post: 03-31-2011, 02:54 PM
  2. no return- problem with actionlistener
    By lparadiso in forum AWT / Swing
    Replies: 2
    Last Post: 11-21-2010, 02:31 AM
  3. Problem Actionlistener
    By Questionmark in forum New To Java
    Replies: 18
    Last Post: 08-07-2010, 12:29 PM
  4. Problem w/ ActionListener on a button
    By qwertyuiop23 in forum AWT / Swing
    Replies: 2
    Last Post: 11-02-2009, 06:25 AM
  5. ActionListener Applet problem
    By xander5511 in forum Java Applets
    Replies: 1
    Last Post: 02-21-2009, 03:42 AM

Posting Permissions

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