This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Troble working with JButtons..

 
Scott Cajun
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Baisical its a marine simulation, and i am coing a GUI where there are 3 buttons to feed 3 different fish, bt i cannot seem to get the right code layout to get it to work, can you try and see what the problem is?



it compiles.. but the button doesn't do anything
also, if i try to add:
feed1.addActionListener(this);
feed2.addActionListener(this);
feed3.addActionListener(this);

it gives me a compile error... "addActionListener(java.awt.Event.ActionListener) in javax.swing.AbstactButton cannot be applied to (InterfaceFrame)"
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> it compiles.. but the button doesn't do anything

the button/s need a listener before they can do anything.

addActionListener(this) needs the class to implement the ActionListener interface
i.e. class InterfaceFrame extends Frame implements ActionListener
and then your code goes into actionPerformed(..), but this style of using
ActionListener is not the best.

Either create a separate class that is an ActionListener
e.g. class FeedListener implements ActionListener
or use anonymous inner classes that have a specific task
e.g. feed1.addActionListener(new ActionListener(){...


this part of your code may cause you painting problems
class InterfaceFrame extends Frame
JButton feed1 = new JButton("Feed Fish 1");

Frame is an AWT component, and JButton is a Swing Component.
Mixing the two is not recommended
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic