Week13 - Chapter 14 Basic Graphical User Interface...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 14 Basic Graphical User Interface Components There is a large amount of material in the next few chapters. My notes will provide you with many examples of different components. However, your primary content responsibility will be on components associated with the programming assignments. For this reason, during the next few classes, I will primarily focus on the components most relevant to your assignments, skipping material that is interesting, yet not as relevant. Using Buttons The most widely used control is the push button. A push button is a component that contains a label and that generates an event when it is pressed. Here is an example of what push buttons look like
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Push buttons are objects of type Button . Button defines these two constructors: Button( ) Button(String str ) After a button has been created, you can set its label by calling setLabel( ) . You can retrieve its label by calling getLabel( ) . These methods are as follows: void setLabel(String str ) String getLabel( ) Each time a button is pressed, action( ) is called. The target field of its Event parameter contains a reference to the button that generated the action. Its Object parameter contains a reference to the string that is the label of the button. Usually, either value may be used to identify the button, as you will see in examples that follow. Here is an example that creates three buttons labeled "Yes," "No," and "Undecided." Each time one is pressed, a message is displayed that reports which button has been pressed. In this version, the label of the button is used to determine which button has been pressed. creates an empty button a button containing str str becomes new label
Background image of page 2
// Demonstrate Buttons import java.awt.*; import java.applet.*; /* <applet code="ButtonDemo" width=250 height=150> </applet> */ public class ButtonDemo extends Applet { String msg = ""; public void init( ) { Button yes = new Button("Yes"); Button no = new Button("No"); Button maybe = new Button("Undecided"); add(yes); add(no); add(maybe); } // Recognize buttons by their labels. public boolean action(Event evtObj, Object arg) { if (evtObj.target instanceof Button) { if (arg.equals("Yes")) msg = "You pressed Yes."; else if (arg.equals("No")) msg="You pressed No."; else if (arg.equals("Undecided")) msg="You pressed Undecided."; repaint( ); return true; } return false; } public void paint (Graphics g) { g.drawString(msg, 6, 100); } }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sometimes it is useful to know the type of an object during run time. Inside action( ) , notice that the outer if statement checks if the object that generated the event is a button by using the instanceof operator. instanceof has the following form: object instanceof type Here, object is an instance of a class, and type is a class type. If object is of the specified type or can be cast into the specified type, then the instanceof operator evaluates to true .
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/12/2009 for the course CS 120 taught by Professor Robertdependahl during the Fall '09 term at Santa Barbara City.

Page1 / 34

Week13 - Chapter 14 Basic Graphical User Interface...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online