P. 1
OOP2 with JAVA

OOP2 with JAVA


|Views: 1,640|Likes:
Published by semillastan

More info:

Published by: semillastan on Jul 09, 2008
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





An object of class JButton is a push button that the user can click to trigger some
action. You’ve already seen buttons, but we consider them in much more detail here.
To use any component effectively, there are several aspects of the corresponding class
that you should be familiar with. For JButton, as an example, I list these aspects

Constructors: The JButton class has a constructor that takes a string as a
parameter. This string becomes the text displayed on the button. For example
constructing the JButton with stopGoButton = new JButton(‘‘Go’’) creates
a button object that will display the text, “Go” (but remember that the button
must still be added to a container before it can appear on the screen).

Events: When the user clicks on a button, the button generates an event of
type ActionEvent. This event is sent to any listener that has been registered
with the button as an ActionListener.

Listeners: An object that wants to handle events generated by buttons must
implementtheActionListenerinterface. Thisinterfacedefinesjustonemethod,
“pubic void actionPerformed(ActionEvent evt)”,
which is called to notify the object of an action event.

RegistrationofListeners: In order to actually receive notification of an event
from a button, an ActionListener must be registered with the button. This is
done with the button’s addActionListener() method. For example:
stopGoButton.addActionListener( buttonHandler);

Event methods: When actionPerformed(evt) is called by the button, the pa-
rameter, evt, contains information about the event. This information can be re-
trieved by calling methods in the ActionEvent class. In particular,


the button. By default, this command is the text that is displayed on the button,
but it is possible to set it to some other string. The method evt.getSource() re-
turns a reference to the Object that produced the event, that is, to the JButton
that was pressed. The return value is of type Object, not JButton, because
other types of components can also produce ActionEvents.

Component methods: Several useful methods are defined in the JButton
class. For example, stopGoButton.setText(‘‘Stop’’) changes the text dis-
playedonthebuttonto“Stop”. AndstopGoButton.setActionCommand(‘‘sgb’’)
changes the action command associated to this button for action events.

Ofcourse, JButtonshaveallthegeneralComponentmethods, suchassetEnabled()
and setFont(). The setEnabled() and setText() methods of a button are particu-
larly useful for giving the user information about what is going on in the program. A
disabled button is better than a button that gives an obnoxious error message such
as “Sorry, you can’t click on me now!”

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->