Buttons Appearance of buttons A button has one
Buttons
Appearance of buttons • A button has one of three appearances: • Disabled 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 Flow. Layout set. Layout(new Flow. Layout( )); Button button 1 = new Button("Button 1"); add(button 1);
Placing buttons with Grid. Layout set. Layout(new Grid. Layout(2, 3)); Button button 1 = new Button("Button 1"); add(button 1);
Placing buttons with Border. Layout set. Layout(new Border. Layout( )); Button button 1 = new Button("Button 1"); add(button 1, Border. Layout. NORTH);
Using a layout manager • Create a container (usually a Panel) • Send it the message set. Layout(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 Border. Layout, an extra parameter should be used • Example: 1. Panel p = new Panel(); p. set. Layout(new Grid. Layout(3, 4)); p. add(button 1);
Adding a listener import java. awt. event. *; button 1. add. Action. Listener(new My. Button. Listener()); class My. Button. Listener implements Action. Listener { public void action. Performed(Action. Event e) { code to execute when button 1 is pressed } } • My. Button. Listener 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 My. Listener listener = new My. Button. Listener(); button 1. add. Action. Listener(listener); button 2. add. Action. Listener(listener); button 3. add. Action. Listener(listener); class My. Button. Listener implements Action. Listener { public void action. Performed(Action. Event e) { code to execute when button 1 is pressed } }
Adding an anonymous listener import java. awt. event. *; button 1. add. Action. Listener(new Action. Listener(){ public void action. Performed(Action. Event e) { code to execute when button 1 is pressed }}); • An anonymous listener is more convenient if: – The action. Performed 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 set. Enabled(boolean b) • To enable a button: button 1. set. Enabled(true); • To disable a button: button 1. set. Enabled(false); • You will never get an Event from a disabled component
Changing the button’s appearance • button. set. Label(String label); – Changes the label on a button • button. set. Background(Color color); button. set. Foreground(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 "Dialog. Input" – 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. set. Font(Font font); or panel. set. Font(Font font); // default for contents
Writing the listener • To listen for a button click: – Write a class that implements Action. Listener – Create an instance of that class – Attach the instance to one or more buttons button 1. add. Action. Listener(new My. Button. Listener()); • To implement Action. Listener, you must provide this method: public void action. Performed(Action. Event e)
Writing action. Performed • action. Performed must be public void • action. Performed takes an Action. Event parameter • If the listener is attached to only a single button, you can ignore the Action. Event • If the listener is attached to several buttons, you can use the Action. Event parameter to discover which button was pressed
Examining an Action. Event public void action. Performed(Action. Event e) • An Action. Event is an Event. Object – it inherits a method public Object get. Source() – get. Source() returns the Object that caused the event – So: if (e. get. Source() == button 1) {. . . } • Alternatively, an Action. Event has a method public String get. Action. Command() that (for a Button) returns the label on the button – So: if (e. get. Action. Command(). 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
- Slides: 20