Transcript Buttons

Buttons
Appearance of buttons
• A button has one of three appearances:
• Disabled by your program
• Enabled by your program
• Enabled by your program
and pressed by the user
A style rule for buttons
• Users expect buttons to do things
• When the user clicks a button, there should be a
visible change in the display
– If there isn’t, the user wonders “Did the program
recognize my button click?”
• This is an application of the Principle of Least
Surprise: A program should surprise the user as
little as possible.
– In other words: a program should behave the way the
user expects it to behave
Calculator example
• In the Calculator program, every button click
changes the display
– Almost: Clicking the Clear button repeatedly leaves the
display at 0 (duh!)
• Clicking a base button (Oct, Dec, Hex) changes
the number in the display
–
–
–
–
Unless it’s a small number (less or equal to base)
Even then, some buttons are enabled/disabled
This is a subtle change that might not be noticed
It would be better to display Oct/Dec/Hex somewhere
Button constructors
import java.awt.*;
new Button()
– Constructs a button with no label
new Button(String label)
– Constructs a button with the given label
Placing buttons with FlowLayout
setLayout(new FlowLayout( ));
Button button1 = new Button("Button 1");
add(button1);
Placing buttons with GridLayout
setLayout(new GridLayout(2, 3));
Button button1 = new Button("Button 1");
add(button1);
Placing buttons with BorderLayout
setLayout(new BorderLayout( ));
Button button1 = new Button("Button 1");
add(button1, BorderLayout.NORTH);
Using a layout manager
• Create a container (usually a Panel)
• Send it the message setLayout(layout_manager)
to tell it what kind of layout manager to use
• Send add messages to the container; the kind of
add message depends on the layout manager
– If BorderLayout, an extra parameter should be used
• Example:
Panel p = new Panel();
p.setLayout(new GridLayout(3, 4));
p.add(button1);
Adding a listener
import java.awt.event.*;
button1.addActionListener(new MyButtonListener());
class MyButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
code to execute when button1 is pressed
}
}
• MyButtonListener is best implemented as a member class, so
that it has full access to the fields of the enclosing class
Adding the same listener to
several buttons
MyListener listener = new MyButtonListener();
button1.addActionListener(listener);
button2.addActionListener(listener);
button3.addActionListener(listener);
class MyButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
code to execute when button1 is pressed
}
}
Adding an anonymous listener
import java.awt.event.*;
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
code to execute when button1 is pressed
}});
• An anonymous listener is more convenient if:
– The actionPerformed method is short, and
– The listener is only used for this one button
Enabling the button
• A Button is a Component
• A Component can be enabled or disabled
• Button inherits this method from Component:
public void setEnabled(boolean b)
• To enable a button:
button1.setEnabled(true);
• To disable a button:
button1.setEnabled(false);
• You will never get an Event from a disabled
component
Changing the button’s appearance
• button.setLabel(String label);
– Changes the label on a button
• button.setBackground(Color color);
button.setForeground(Color color);
– Changes color of background or text of button
– These methods are inherited from Component
– May not work on all platforms
Changing the button’s font
new Font(String name, int style, int size)
– name: The name of a font. It may be:
• A font on your system (maybe not on my system), or
• one of the font types "Serif", "Sans-serif",
"Monospaced", "Dialog", and "DialogInput"
– style: one of Font.PLAIN, Font.BOLD, Font.ITALIC, or
Font.BOLD+Font.ITALIC
– size: The point size, such as 10, 12, or 18
button.setFont(Font font); or
panel.setFont(Font font); // default for contents
Writing the listener
• To listen for a button click:
– Write a class that implements ActionListener
– Create an instance of that class
– Attach the instance to one or more buttons
button1.addActionListener(new MyButtonListener());
• To implement ActionListener, you must
provide this method:
public void actionPerformed(ActionEvent e)
Writing actionPerformed
• actionPerformed must be public void
• actionPerformed takes an ActionEvent
parameter
• If the listener is attached to only a single
button, you can ignore the ActionEvent
• If the listener is attached to several buttons,
you can use the ActionEvent parameter to
discover which button was pressed
Examining an ActionEvent
public void actionPerformed(ActionEvent e)
• An ActionEvent is an EventObject
– it inherits a method public Object getSource()
– getSource() returns the Object that caused the event
– So: if (e.getSource() == button1) {...}
• Alternatively, an ActionEvent has a method public
String getActionCommand() that (for a Button)
returns the label on the button
– So: if (e.getActionCommand().equals("Button 1")) {...}
Doing something
• You define what the button does in your
button listener
• If the listener is an instance of a member
class or an anonymous class, you have full
access to the enclosing class
• If the listener is an external class, it may be
harder to access the information you need
• Good luck!
The End