Assignment 1
Assignment 1
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.
import java.awt.*;
import java.awt.event.*;
Button loginButton;
public LoginForm() {
// Create components
passwordField.setEchoChar('*');
// Set layout
setLayout(new FlowLayout());
add(usernameLabel);
add(usernameField);
add(passwordLabel);
add(passwordField);
add(loginButton);
add(messageLabel);
loginButton.addActionListener(this);
// Frame settings
setTitle("Login Form");
setSize(300, 150);
setVisible(true);
addWindowListener(new WindowAdapter() {
System.exit(0);
});
@Override
// Simple validation
messageLabel.setText("Login successful!");
} else {
messageLabel.setText("Invalid credentials!");
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.
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.
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.
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
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
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"
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.
import java.awt.*;
import java.awt.event.*;
public MenuBarExample() {
fileMenu.add(new MenuItem("New"));
fileMenu.add(new MenuItem("Open"));
fileMenu.add(new MenuItem("Save"));
fileMenu.addSeparator();
fileMenu.add(exitItem);
menuBar.add(fileMenu);
// Create "Edit" menu
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);
new MenuBarExample();