1 of 10

Adanced Java Programming

AWT User Interface Controls

Smt.M.Jeevana Sujitha

Assistant Professor

Department of Computer Science and Engineering

SRKR Engineering College, Bhimavaram, A.P. - 534204

2 of 10

OBJECTIVES

The Objective of this lecture is

  • To learn about AWT user interface controls.

3 of 10

AWT User Interface Controls

  • TextField: The object of a TextField is text component

that allows us to write a single line text.

Example:

TextField t1=new TextField(10);

Method and Description

public void add(Component c)

inserts a component on this component.

public void setSize(int width,int height)

sets the size (width and height) of the component.

public void setLayout(LayoutManager m)

defines the layout manager for the component.

public void setVisible(boolean status)

changes the visibility of the component, by default false.

setBounds(int x-coordinate, int y-coordinate, int width, int height)

The first two arguments are x and y coordinates of the top-left corner of the component, the third argument is the width of the component and the fourth argument is the height of the component.

4 of 10

Example program

import java.awt.*;

class TextFieldExample

{

public static void main(String args[])

{

Frame f= new Frame("TextField Example");

TextField t1,t2;

t1=new TextField("Welcome to Java awt");

t1.setBounds(50,100, 200,30);

t2=new TextField("AWT Tutorial");

t2.setBounds(50,150, 200,30);

f.add(t1);

f.add(t2);

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

}

}

5 of 10

AWT User Interface Controls

import java.awt.*;

class LabelExample

{

public static void main(String args[])

{

Frame f= new Frame("Label Example");

Label l1,l2;

l1=new Label("First Label");

l1.setBounds(50,100,100,30);

l2=new Label("Second Label");

l2.setBounds(50,150,100,30);

f.add(l1);

f.add(l2);

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

}

}

  • Label: It is used to display a single line of read only text.

The text can be changed by an application but an end user

can not edit it directly.

Example:

Label l1=new Label(“Enter user name”);

6 of 10

AWT User Interface Controls

Button: The Button class is used to create a labeled

button that has platform independent implementation.

Example:

Button b1=new Button(“login”);

7 of 10

AWT User Interface Controls

  • TextArea: It is used to enter a multi-line text.

Example:

TextArea ta=new TextArea(“palakollu”);

  • Checkbox: The Checkbox class is used to create a checkbox. It

is used when more than one option is required to be selected.

Example:

Checkbox c1=new Checkbox(“cse", true);

  • CheckboxGroup (Radio button): There is no special control for

creating radio button in AWT. But we can create it by using

CheckboxGroup.

Example:

CheckboxGroup cg=new CheckboxGroup(); Checkbox c1=new Checkbox(“male”, cg, true); Checkbox c2=new Checkbox(“female”, cg, false);

8 of 10

AWT User Interface Controls

  • List: It represents a list of text items. By using the List user

can choose either one or multiple items.

Example:

List l=new List(3,true); l.add(“c”);

l.add(“cpp”);

l.add(“java”);

l.add(“wt”);

  • Choice: A Choice is used when we want to select a single option

from a list of available choices.

Example:

Choice ch=new Choice(); ch.add(“hyd”);

ch.add(“pkl”);

ch.add(“bvrm”)

9 of 10

AWT User Interface Controls

  • Scrollbar: It is used to add horizontal and vertical scrollbar. It is a GUI component allows us to see invisible number of rows and columns

Example:

Scrollbar s=new Scrollbar(); s.setBounds(100,100,50,50); add(s);

10 of 10

THANK YOU