DOC PREVIEW
Penn CIT 591 - Buttons

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

ButtonsAppearance of buttonsA style rule for buttonsCalculator exampleButton constructorsPlacing buttons with FlowLayoutPlacing buttons with GridLayoutPlacing buttons with BorderLayoutUsing a layout managerAdding a listenerAdding the same listener to several buttonsAdding an anonymous listenerEnabling the buttonChanging the button’s appearanceChanging the button’s fontWriting the listenerWriting actionPerformedExamining an ActionEventDoing somethingThe EndButtonsAppearance 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 userA 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 behaveCalculator 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 somewhereButton constructors import java.awt.*; new Button()–Constructs a button with no label new Button(String label)–Constructs a button with the given labelPlacing 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 listenerimport 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 classAdding the same listener to several buttonsMyListener 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 listenerimport 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 buttonEnabling 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 componentChanging 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 platformsChanging 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 contentsWriting 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 pressedExamining 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


View Full Document

Penn CIT 591 - Buttons

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Download Buttons
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Buttons and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Buttons 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?