• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Troble working with JButtons..

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)"
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic