Results 1 to 6 of 6
- 02-22-2012, 06:44 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 9
- Rep Power
- 0
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 :)"); } // } }
- 02-22-2012, 06:55 PM #2
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.writing this line of code in go() method give me an error.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-22-2012, 06:59 PM #3
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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!
- 02-22-2012, 09:02 PM #4
Member
- Join Date
- Feb 2012
- Posts
- 9
- Rep Power
- 0
Re: Have problem with add.ActionListener()
How about line 33 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!How about posting the specific details of the error? Most of us here don't like playing guessing games.
- 02-22-2012, 09:16 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 9
- Rep Power
- 0
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?
-
Re: Have problem with add.ActionListener()
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
-
Problem with ActionListener?
By Manish87 in forum AWT / SwingReplies: 5Last Post: 03-31-2011, 02:54 PM -
no return- problem with actionlistener
By lparadiso in forum AWT / SwingReplies: 2Last Post: 11-21-2010, 02:31 AM -
Problem Actionlistener
By Questionmark in forum New To JavaReplies: 18Last Post: 08-07-2010, 12:29 PM -
Problem w/ ActionListener on a button
By qwertyuiop23 in forum AWT / SwingReplies: 2Last Post: 11-02-2009, 06:25 AM -
ActionListener Applet problem
By xander5511 in forum Java AppletsReplies: 1Last Post: 02-21-2009, 03:42 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks