+ All Categories
Home > Documents > Labels and buttons

Labels and buttons

Date post: 15-May-2015
Category:
Upload: myrajendra
View: 716 times
Download: 0 times
Share this document with a friend
Popular Tags:
22
http:// improvejava.blogspot.in/ Labels and buttons 1
Transcript
Page 1: Labels and buttons

http://improvejava.blogspot.in/

Labels and buttons

1

Page 2: Labels and buttons

http://improvejava.blogspot.in/

On completion of this period, you would be able to know

• Control fundamentals• Adding and removing controls• Labels • Buttons

2

Objectives

Page 3: Labels and buttons

http://improvejava.blogspot.in/

Recap

3

In the previous class, you have leant

• Displaying information within a window

• Working with color and font

Page 4: Labels and buttons

http://improvejava.blogspot.in/

Control Fundamentals

• The AWT supports the following types of controls:• Labels• Push buttons• Check boxes• Choice lists• Lists• Scroll bars• Text editing

• These controls are subclasses of Component

4

Page 5: Labels and buttons

http://improvejava.blogspot.in/

Adding and Removing Controls

• To include a control in a window, you must add it to the window

• To do this, you must first create an instance of the desired control

• Then add it to a window by calling add()• The syntax is

– Component add(Component compObj);– Here, compObj is an instance of the control that you

want to add– A reference to compObj is returned

5

Page 6: Labels and buttons

http://improvejava.blogspot.in/

• Sometimes you want to remove a control from a window when the control is no longer needed

• To do this, call remove( ) • It has this general form:

– void remove(Component obj)– Here, obj is a reference to the control you want to

remove

• You can remove all controls by calling removeAll( )

Adding and Removing Controls contd..

6

Page 7: Labels and buttons

http://improvejava.blogspot.in/

Labels

• The easiest control to use is a label• A label is an object of type Label, and it contains

a string, which it displays• Labels are passive controls that do not support

any interaction• with the user• Label defines the following constructors:

– Label( )– Label(String str)– Label(String str, int how)

7

Page 8: Labels and buttons

http://improvejava.blogspot.in/

Labels contd..

• The first version creates a blank label• The second version creates a label that contains

the string specified by str• This string is left-justified• The third version creates a label that contains

the string specified by str using the alignment specified by ‘how’

8

Page 9: Labels and buttons

http://improvejava.blogspot.in/

• The alignment is specified by one of these three constants– Label.LEFT– Label.RIGHT– Label.CENTER

• You can set or change the text in a label by using the setText( ) method– void setText(String str)

Labels contd..

9

Page 10: Labels and buttons

http://improvejava.blogspot.in/

• You can obtain the current label by calling getText( )– String getText( )

• You can set the alignment of the string within the label by calling setAlignment()– void setAlignment(int how)

• To obtain the current alignment, call getAlignment( )– int getAlignment( )

Labels contd..

10

Page 11: Labels and buttons

http://improvejava.blogspot.in/

// Demonstrate Labelsimport java.awt.*;import java.applet.*;/*<applet code="LabelDemo" width=300 height=200></applet>*/public class LabelDemo extends Applet {

public void init() {Label one = new Label("One");Label two = new Label("Two");Label three = new Label("Three");// add labels to applet windowadd(one);add(two);add(three);

}}

Labels contd..

The sample output is shown here

Fig. 70.1 LabelDemo

11

Page 12: Labels and buttons

http://improvejava.blogspot.in/

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

• Push buttons are objects of type Button

12

Page 13: Labels and buttons

http://improvejava.blogspot.in/

• Button defines these two constructors:– Button( )– Button(String str)– The first version creates an empty button– The second creates a button that contains str as a label

• After a button has been created, you can set its label by calling setLabel( )– void setLabel(String str)

– Here, str becomes the new label for the button

Buttons contd..

13

Page 14: Labels and buttons

http://improvejava.blogspot.in/

Handling Buttons

• Each time a button is pressed, an action event is generated

• This is sent to any listeners that previously registered • Each listener implements the ActionListener interface• That interface defines the actionPerformed() method,

which is called when an event occurs• We have to write our code in this method to handle the

button’s event

14

Page 15: Labels and buttons

http://improvejava.blogspot.in/

// Demonstrate Buttonsimport java.awt.*;import java.awt.event.*;import java.applet.*;/*<applet code="ButtonDemo" width=250 height=150></applet>*/public class ButtonDemo extends Applet implements ActionListener {

String msg = "";Button yes, no, maybe;public void init() {yes = new Button("Yes");no = new Button("No");maybe = new Button("Undecided");

Handling Buttons contd..

15

Page 16: Labels and buttons

http://improvejava.blogspot.in/

add(yes);

add(no);

add(maybe);

yes.addActionListener(this);

no.addActionListener(this);

maybe.addActionListener(this);

}

Handling Buttons contd..

16

Page 17: Labels and buttons

http://improvejava.blogspot.in/

public void actionPerformed(ActionEvent ae) {String str = ae.getActionCommand();if(str.equals("Yes")) {

msg = "You pressed Yes.";} else if(str.equals("No")) {

msg = "You pressed No.";} else {

msg = "You pressed Undecided.";}repaint();

}

Handling Buttons contd..

17

Page 18: Labels and buttons

http://improvejava.blogspot.in/

public void paint(Graphics g) {

g.drawString(msg, 6, 100);

}

} The sample output is shown here

Fig. 70.2 Handling Buttons

Handling Buttons contd..

18

Page 19: Labels and buttons

http://improvejava.blogspot.in/

Summary

• AWT supports several GUI components

• In this class we have seen how to use – Labels– Buttons

19

Page 20: Labels and buttons

http://improvejava.blogspot.in/

Quiz

1. The event generated by button is– FocusEvent– ActionEvent– ContainerEvent– KeyEvent

20

Page 21: Labels and buttons

http://improvejava.blogspot.in/

Quiz contd..

2. Which method is used get the caption of a label– getCaption()– getLabel()– getText()– None

21

Page 22: Labels and buttons

http://improvejava.blogspot.in/

Frequently Asked Questions

1. List the constructors and methods of Label

2. List the constructors and methods of Button

22


Recommended