0% found this document useful (0 votes)
7 views13 pages

AWT Components in Java: Overview and Examples

The document provides an overview of AWT components in Java, including Labels, Buttons, TextFields, TextAreas, Checkboxes, CheckboxGroups, Choices, Lists, Scrollbars, Frames, and Layouts (FlowLayout, BorderLayout, GridLayout). Each component is defined with its syntax, constructors, methods, and example code demonstrating its usage. This serves as a guide for creating graphical user interfaces using Java AWT.

Uploaded by

sk1995sayma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views13 pages

AWT Components in Java: Overview and Examples

The document provides an overview of AWT components in Java, including Labels, Buttons, TextFields, TextAreas, Checkboxes, CheckboxGroups, Choices, Lists, Scrollbars, Frames, and Layouts (FlowLayout, BorderLayout, GridLayout). Each component is defined with its syntax, constructors, methods, and example code demonstrating its usage. This serves as a guide for creating graphical user interfaces using Java AWT.

Uploaded by

sk1995sayma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SAM GIRLS COLLEGE, BHOPAL

(Affiliated to Barkatullah University)


Internet Application Using Java
UNIT – 3
AWT COMPONENTS
1. Label

Definition

A Label is a non-editable text component used to display information or messages to the user.

Syntax : Label label = new Label("Text");

Class constructors

S.N. Constructor & Description

Label()
1
Constructs an empty label.

Label(String text)
2
Constructs a new label with the specified string of text, left justified.

Label(String text, int alignment)


3
Constructs a new label that presents the specified string of text with the specified alignment.

Class methods

S.N. Method & Description

void addNotify()
1
Creates the peer for this label.

AccessibleContext getAccessibleContext()
2
Gets the AccessibleContext associated with this Label.

int getAlignment()
3
Gets the current alignment of this label.

String getText()
4
Gets the text of this label.

5 protected String paramString()


Returns a string representing the state of this Label.

void setAlignment(int alignment)


6
Sets the alignment for this label to the specified alignment.

void setText(String text)


7
Sets the text for this label to the specified text.

Example

import [Link].*;

class LabelDemo {

public static void main(String args[]) {

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

Label l = new Label("Welcome to Java AWT");

[Link](l);

[Link](300,150);

[Link](true);

2. Button

Definition

A Button is used to perform an action when clicked by the user.

Syntax

Button b = new Button("Click");

Class constructors

S.N. Constructor & Description

Button()
1
Constructs a button with an empty string for its label.

Button(String text)
2
Constructs a new button with specified label.

Class methods
S.N. Method & Description

void addActionListener(ActionListener l)
1
Adds the specified action listener to receive action events from this button.

void addNotify()
2
Creates the peer of the button.

AccessibleContext getAccessibleContext()
3
Gets the AccessibleContext associated with this Button.

String getActionCommand()
4
Returns the command name of the action event fired by this button.

ActionListener[] getActionListeners()
5
Returns an array of all the action listeners registered on this button.

String getLabel()
6
Gets the label of this button.

void removeActionListener(ActionListener l)
7
Removes the specified action listener so that it no longer receives action events from this button.

void setActionCommand(String command)


8
Sets the command name for the action event fired by this button.

void setLabel(String label)


9
Sets the button's label to be the specified string.

Example

import [Link].*;

class ButtonDemo {

public static void main(String args[]) {

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

Button b = new Button("Submit");

[Link](b);

[Link](300,150);

[Link](true);

}
}

3. TextField

Definition

A TextField allows the user to enter single-line text input.

Syntax

TextField tf = new TextField(20);

Class constructors

S.N. Constructor & Description

TextField()
1
Constructs a new text field.

TextField(int columns)
2
Constructs a new empty text field with the specified number of columns.

TextField(String text)
3
Constructs a new text field initialized with the specified text.

TextField(String text, int columns)


4 Constructs a new text field initialized with the specified text to be displayed, and wide enough to hold the
specified number of columns.

Class methods

S.N. Method & Description

void addActionListener(ActionListener l)
1
Adds the specified action listener to receive action events from this text field.

void addNotify()
2
Creates the TextField's peer.

int getColumns()
3
Gets the number of columns in this text field.

char getEchoChar()
4
Gets the character that is to be used for echoing.
void setText(String t)
5
Sets the text that is presented by this text component to be the specified text.

Example

import [Link].*;

class TextFieldDemo {

public static void main(String args[]) {

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

TextField tf = new TextField(20);

[Link](tf);

[Link](300,150);

[Link](true);

4. TextArea

Definition

A TextArea allows the user to enter multiple lines of text.

Syntax

TextArea ta = new TextArea(rows, columns);

Class constructors

S.N. Constructor & Description

TextArea()
1
Constructs a new text area with the empty string as text.

TextArea(int rows, int columns)


2
Constructs a new text area with the specified number of rows and columns and the empty string as text.

TextArea(String text)
3
Constructs a new text area with the specified text.

TextArea(String text, int rows, int columns)


4
Constructs a new text area with the specified text, and with the specified number of rows and columns.
TextArea(String text, int rows, int columns, int scrollbars)
5 Constructs a new text area with the specified text, and with the rows, columns, and scroll bar visibility as
specified.

Example

import [Link].*;

class TextAreaDemo {

public static void main(String args[]) {

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

TextArea ta = new TextArea(5, 20);

[Link](ta);

[Link](300,200);

[Link](true);

5. Checkbox

Definition

A Checkbox allows the user to select one or more options.

Syntax

Checkbox cb = new Checkbox("Option");

Class constructors

S.N. Constructor & Description

Checkbox()
1
Creates a check box with an empty string for its label.

Checkbox(String label)
2
Creates a check box with the specified label.

Checkbox(String label, boolean state)


3
Creates a check box with the specified label and sets the specified state.

Checkbox(String label, boolean state, CheckboxGroup group)


4
Constructs a Checkbox with the specified label, set to the specified state, and in the specified check box g
Checkbox(String label, CheckboxGroup group, boolean state)
5
Creates a check box with the specified label, in the specified check box group, and set to the specified stat

Example

import [Link].*;

class CheckboxDemo {

public static void main(String args[]) {

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

Checkbox c1 = new Checkbox("Java");

Checkbox c2 = new Checkbox("Python");

[Link](new FlowLayout());

[Link](c1);

[Link](c2);

[Link](300,150);

[Link](true);

6. CheckboxGroup (Radio Button)

Definition

A CheckboxGroup allows the user to select only one option at a time.

Syntax

CheckboxGroup cg = new CheckboxGroup();

Checkbox cb = new Checkbox("Option", cg, true);

Class constructors

S.N. Constructor & Description

CheckboxGroup() ()
1
Creates a new instance of CheckboxGroup.

Class methods

S.N. Method & Description

1 Checkbox getCurrent()
Deprecated. As of JDK version 1.1, replaced by getSelectedCheckbox().

Checkbox getSelectedCheckbox()
2
Gets the current choice from this check box group.

void setCurrent(Checkbox box)


3
Deprecated. As of JDK version 1.1, replaced by setSelectedCheckbox(Checkbox).

void setSelectedCheckbox(Checkbox box)


4
Sets the currently selected check box in this group to be the specified check box.

String toString()
5
Returns a string representation of this check box group, including the value of its current selection.

Example

import [Link].*;

class CheckboxGroupDemo {

public static void main(String args[]) {

Frame f = new Frame("Radio Button Example");

CheckboxGroup cg = new CheckboxGroup();

Checkbox c1 = new Checkbox("Male", cg, true);

Checkbox c2 = new Checkbox("Female", cg, false);

[Link](new FlowLayout());

[Link](c1);

[Link](c2);

[Link](300,150);

[Link](true);

7. Choice

Definition

A Choice is a drop-down list that allows the user to select only one item.

Syntax
Choice ch = new Choice();

[Link]("Item");

Class constructors

S.N. Constructor & Description

Choice() ()
1
Creates a new choice menu.

Example

import [Link].*;

class ChoiceDemo {

public static void main(String args[]) {

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

Choice ch = new Choice();

[Link]("India");

[Link]("USA");

[Link]("UK");

[Link](ch);

[Link](300,150);

[Link](true);

8. List

Definition

A List displays a list of items and allows single or multiple selection.

Syntax

List li = new List(rows, multipleSelection);

Class constructors

S.N. Constructor & Description

List()
1
Creates a new scrolling list.
List(int rows)
2
Creates a new scrolling list initialized with the specified number of visible lines.

List(int rows, boolean multipleMode)


3
Creates a new scrolling list initialized to display the specified number of rows.

Example

import [Link].*;

class ListDemo {

public static void main(String args[]) {

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

List li = new List(4, true);

[Link]("C");

[Link]("C++");

[Link]("Java");

[Link]("Python");

[Link](li);

[Link](300,200);

[Link](true);

9. Scrollbar

Definition

A Scrollbar allows the user to scroll content vertically or horizontally.

Syntax

Scrollbar sb = new Scrollbar([Link], value, visible, min, max);

Class constructors

S.N. Constructor & Description

Scrollbar()
1
Constructs a new vertical scroll bar.

2 Scrollbar(int orientation)
Constructs a new scroll bar with the specified orientation.

Scrollbar(int orientation, int value, int visible, int minimum, int maximum)
3 Constructs a new scroll bar with the specified orientation, initial value, visible amount, and minimum and
maximum values.

Example

import [Link].*;

class ScrollbarDemo {

public static void main(String args[]) {

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

Scrollbar sb = new Scrollbar([Link], 0, 10, 0, 100);

[Link](sb);

[Link](200,200);

[Link](true);

10. Frame

Definition

A Frame is a top-level window with title bar and border.

Syntax

Frame f = new Frame("Title");

Example

import [Link].*;

class FrameDemo {

public static void main(String args[]) {

Frame f = new Frame("My Frame");

[Link](300,200);

[Link](true);

11. FlowLayout
Definition

FlowLayout arranges components left to right in a row.

Syntax

setLayout(new FlowLayout());

Example

import [Link].*;

class FlowLayoutDemo {

public static void main(String args[]) {

Frame f = new Frame("FlowLayout");

[Link](new FlowLayout());

[Link](new Button("One"));

[Link](new Button("Two"));

[Link](300,200);

[Link](true);

12. BorderLayout

Definition

BorderLayout divides container into five regions.

Syntax

add(component, [Link]);

Example

import [Link].*;

class BorderLayoutDemo {

public static void main(String args[]) {

Frame f = new Frame("BorderLayout");

[Link](new Button("North"), [Link]);

[Link](new Button("Center"), [Link]);

[Link](300,200);

[Link](true);

}
13. GridLayout

Definition

GridLayout arranges components in rows and columns of equal size.

Syntax

setLayout(new GridLayout(rows, cols));

Example

import [Link].*;

class GridLayoutDemo {

public static void main(String args[]) {

Frame f = new Frame("GridLayout");

[Link](new GridLayout(2,2));

[Link](new Button("1"));

[Link](new Button("2"));

[Link](new Button("3"));

[Link](new Button("4"));

[Link](300,200);

[Link](true);

You might also like