0% found this document useful (0 votes)
12 views7 pages

Assignment 1

The document explains the Java AWT component hierarchy, detailing various components such as Window, Frame, Applet, and Panel, along with their characteristics. It includes a sample code for a simple Login form using AWT Frame and Applet, and discusses the LayoutManager class and its types. Additionally, it describes several AWT components like Label, TextField, Button, and Checkbox, along with their constructors and functionalities.

Uploaded by

sanskaruravane9
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)
12 views7 pages

Assignment 1

The document explains the Java AWT component hierarchy, detailing various components such as Window, Frame, Applet, and Panel, along with their characteristics. It includes a sample code for a simple Login form using AWT Frame and Applet, and discusses the LayoutManager class and its types. Additionally, it describes several AWT components like Label, TextField, Button, and Checkbox, along with their constructors and functionalities.

Uploaded by

sanskaruravane9
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
You are on page 1/ 7

Assignment 1

1. Explain java.awt.component hierarchy.

All the elements like the button, text fields, scroll bars, etc. are called components. In Java AWT,
there are classes for each component as shown in above diagram. In order to place every component
in a particular position on a screen, we need to add them to a container.

2. Differentiate / Explain :

a. Window

The window is the container that have no borders and menu bars. • You must use frame, dialog or
another window for creating a window. • We need to create an instance of Window class to create
this container

b. Frame

The Frame is the container that contain title bar and border and can have menu bars. • It can have
other components like button, text field, scrollbar etc. • Frame is most widely used container while
developing an AWT application

c. Applet
A Java application that is integrated into a webpage is called an applet. It functions as a front-end and
is run within the web computer. It makes a page more interactive and dynamic by operating inside
the web browser. Applets are hosted on web servers and inserted into HTML pages via the OBJECT or
APPLET tags.

d. Panel

The Panel is the container that doesn't contain title bar, border or menu bar. • It is generic container
for holding the components. • It can have other components like button, text field etc. • An instance
of Panel class creates a container, in which we can add components.

3. Create a simple Login form using AWT Frame & Applet

import java.awt.*;

import java.awt.event.*;

public class LoginForm extends Frame implements ActionListener {

TextField usernameField, passwordField;

Label usernameLabel, passwordLabel, messageLabel;

Button loginButton;

public LoginForm() {

// Create components

usernameLabel = new Label("Username:");

passwordLabel = new Label("Password:");

usernameField = new TextField(20);

passwordField = new TextField(20);

passwordField.setEchoChar('*');

loginButton = new Button("Login");

messageLabel = new Label();

// Set layout

setLayout(new FlowLayout());

// Add components to the frame

add(usernameLabel);
add(usernameField);

add(passwordLabel);

add(passwordField);

add(loginButton);

add(messageLabel);

// Add action listener to the button

loginButton.addActionListener(this);

// Frame settings

setTitle("Login Form");

setSize(300, 150);

setVisible(true);

// Add window listener to close the window

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent windowEvent) {

System.exit(0);

});

@Override

public void actionPerformed(ActionEvent e) {

String username = usernameField.getText();

String password = passwordField.getText();

// Simple validation

if (username.equals("admin") && password.equals("password")) {

messageLabel.setText("Login successful!");

} else {
messageLabel.setText("Invalid credentials!");

public static void main(String[] args) {

new LoginForm();

4. What is the use of LayoutManager class in AWT? List Types of Layout Managers with their
behavior.

The LayoutManagers are used to arrange components in a particular manner. • The Java
LayoutManagers facilitates us to control the positioning and size of the components in GUI forms. •
LayoutManager is an interface that is implemented by all the classes of layout managers.

Java FlowLayout: • The Java FlowLayout class is used to arrange the components in a line, one after
another (in a flow).

Java GridLayout : • The Java GridLayout class is used to arrange the components in a rectangular grid.

Java BorderLayout : • The BorderLayout is used to arrange the components in five regions: north,
south, east, west, and center

Java CardLayout : • The Java CardLayout class manages the components in such a manner that only
one component is visible at a time.

5. Explain following components with its constructors:

a. Label

The object of the Label class is a component for placing text in a container. It is used to display a
single line of read only text.

1. Label() It constructs an empty label.

2. Label(String text) It constructs a label with the given string (left justified by default).

3. Label(String text, int alignement) It constructs a label with the specified string and the specified align

b. TextField
The object of a TextField class is a text component that allows a user to enter a single line text and
edit it. It inherits TextComponent class, which further inherits Component class.

1. TextField() It constructs a new text field component.

2. TextField(String text) It constructs a new text field initialized with the given string text to be displayed

3. TextField(int columns) It constructs a new textfield (empty) with given number of columns.

c. TextArea

The object of a TextArea class is a multiline region that displays text. It allows the editing of multiple
line text. It inherits TextComponent class.

1. TextArea() It constructs a new and empty text area with no text in it.

2. TextArea (int row, int column) It constructs a new text area with specified number of rows and columns and e

d. Button

A button is basically a control component with a label that generates an event when pushed.
The Button class is used to create a labeled button that has platform independent implementation.

1. Button( ) It constructs a new button with an empty string i.e. it has no label.

2. Button (String text) It constructs a new button with given string as its label.

e. Choice

The object of Choice class is used to show popup menu of choices. Choice selected by user is shown
on the top of a menu

1. Choice() It constructs a new choice menu.

f. List

The object of List class represents a list of text items. With the help of the List class, user can choose
either one item or multiple items

1. List() It constructs a new scrolling list.


2. List(int row_num) It constructs a new scrolling list initialized with the given number of rows visible.

g. Checkbox

The Checkbox class is used to create a checkbox. It is used to turn an option on (true) or off (false).
Clicking on a Checkbox changes its state from "on" to "off" or from "off" to "on"

1. Checkbox() It constructs a checkbox with no string as the label.

2. Checkbox(String label) It constructs a checkbox with the given label.

h. CheckboxGroup

The object of CheckboxGroup class is used to group together a set of Checkbox. At a time only one
check box button is allowed to be in "on" state and remaining check box button in "off" state.

6. WAP to add MenuBar to a frame.

import java.awt.*;

import java.awt.event.*;

public class MenuBarExample extends Frame {

public MenuBarExample() {

MenuBar menuBar = new MenuBar();

// Create "File" menu

Menu fileMenu = new Menu("File");

fileMenu.add(new MenuItem("New"));

fileMenu.add(new MenuItem("Open"));

fileMenu.add(new MenuItem("Save"));

fileMenu.addSeparator();

MenuItem exitItem = new MenuItem("Exit");

fileMenu.add(exitItem);

menuBar.add(fileMenu);
// Create "Edit" menu

Menu editMenu = new Menu("Edit");

editMenu.add(new MenuItem("Cut"));

editMenu.add(new MenuItem("Copy"));

editMenu.add(new MenuItem("Paste"));

menuBar.add(editMenu);

setMenuBar(menuBar);

setTitle("MenuBar Example");

setSize(400, 300);

setVisible(true);

public static void main(String[] args) {

new MenuBarExample();

You might also like