Ajp 3VS
Ajp 3VS
Aim: Write a program to demonstrate the use of AWT components like Label,
TextArea, Button, CheckBox, RadioButton, etc.
Theory: AWT is java programming language class library component are visible
objects that can interact with the user. containers (Frame, Panel, Applet) are use to
hold component using in a specific Layout.
Using applet window, design following AWT components using add() method of
component class. Following are some AWT component.
Program Code:
import java.awt.*;
public class Practical_VS1{
public static void main(String[] args) {
Frame f = new Frame("Practical 1");
f.setSize(500, 400);
f.setLayout(null);
Label nameLabel = new Label("Student Name:");
nameLabel.setBounds(50, 50, 100, 30);
TextField name = new TextField();
name.setBounds(150, 50, 200, 30);
Label rollNoLabel = new Label("Roll No:");
rollNoLabel.setBounds(50, 90, 100, 30);
TextField rollNo = new TextField();
rollNo.setBounds(150, 90, 200, 30);
Label enrollmentLabel = new Label("Enrollment No.:");
enrollmentLabel.setBounds(50, 130, 100, 30);
TextField enrollment = new TextField();
enrollment.setBounds(150, 130, 200, 30);
Label casteLabel = new Label("Caste:");
casteLabel.setBounds(50, 170, 100, 30);
Choice caste = new Choice();
caste.add("OBC");
caste.add("SC/ST");
caste.add("OPEN");
caste.add("NT");
caste.setBounds(150, 170, 200, 30);
Label addressLabel = new Label("Address:");
addressLabel.setBounds(50, 210, 100, 30);
TextArea address = new TextArea(10, 3);
address.setBounds(150, 210, 200, 50);
Label genderLabel = new Label("Gender:");
genderLabel.setBounds(50, 270, 100, 30);
CheckboxGroup checkboxGroup = new CheckboxGroup();
Checkbox male = new Checkbox("Male", false, checkboxGroup);
male.setBounds(150, 270, 100, 30);
Checkbox female = new Checkbox("Female", false, checkboxGroup);
female.setBounds(250, 270, 100, 30);
Button submit = new Button("Submit");
submit.setBounds(200, 320, 100, 30);
f.add(nameLabel);
f.add(name);
f.add(rollNoLabel);
f.add(rollNo);
f.add(enrollmentLabel);
f.add(enrollment);
f.add(casteLabel);
f.add(caste);
f.add(addressLabel);
f.add(address);
f.add(genderLabel);
f.add(male);
f.add(female);
f.add(submit);
f.setVisible(true);
}
}
Output:
Aim:
Write a program to design a form using the components List and Choice.
Theory:
What is Choice and List?
The java.awt.Choice component implements a list of items where only the selected
item is displayed.
The list appears as a drop down (pop-down menu) menu can be seen through the
touch of a button built into the component, so it is also known as a check box or
ComboBox. In the same manner as in component java.awt.List; a vertical scrollbar is
automatically displayed when the list can not simultaneously show all the items they
contain. The selection only operate in simple mode i.e. only one item can be selected
at a time, and the choice of an item not selected selects and vice versa.
Choice: A Choice is displayed in a compact form that requires you to pull it down to
see the list of available choices. Only one item may be selected from a Choice.Choice
is the act of picking or deciding between two or more possibilities. Only one item may
be selected from a choice.
List: A List may be displayed in such a way that several List items are visible. A List
supports the selection of one or more List items.A list is any enumeration of a set of
items. A List supports the selection of one or more list items.
• Constructor:
1) Choice()
• Method:
❖ void add(String item); //Items added int the Choice List.
❖ String getSelectedItem();
❖ int getSelectedIndex();
❖ int getItemCount();
❖ void select(int index);
❖ void select(String item);
❖ String getItem(int index);
• Constructor:
❖ List();
❖ List(int visible_rows);
❖ List(int visible_rows,boolean flag);
• Method :
❖ void add(String item);
❖ void add(String item,int index);
❖ String getSelectedItem();
❖ int getSelectedIndex();
❖ String[] getSelectedItems();
Program Code:
import java.awt.*;
class Practical_2VS extends Frame {
public Practical_2VS() {
setLayout(null);
Label sub = new Label("Choose Subjects:");
sub.setBounds(50, 50, 120, 30);
List ls = new List(5, true);
ls.setBounds(180, 50, 150, 120);
ls.add("Java");
ls.add("C++");
ls.add("Python");
ls.add("Operating System");
ls.add("Applied Mathematics");
add(ls);
Output:
Aim: Write a program to design simple calculator with the use of Grid Layout.
Theory:
A layout manager automatically arranges your controls within a window. While it is
possible to layout Java controls by hand, too, you generally won’t. It is very tedious
to manually lay out a large number of components.
Layout Manager is a facility that determines how components should be arranged
when they are added to the container. Layout Manager is an interface that is
implemented by all the classes of layout managers.
There are following classes that represent the layout managers.
Layout Types :
❖ FlowLayout
❖ GridLayout
❖ BorderLayout
❖ CardLayout
❖ GridbagLayout
Grid Layout:
Grid Layout is used to make a bunch of components equal in size and displays them
in the requested number of rows and columns. One component is displayed in each
rectangle. The list of Constructor for GridLayout are:
❖ GridLayout(): creates a grid layout with one column per component in a row.
❖ GridLayout(int rows, int columns): creates a grid layout with the given rows
and columns but no gaps between the components.
❖ GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout with
the given rows and columns along with given horizontal and vertical gaps if
we give setLayout(null) the default layout is disabled.then we have to use
setBounds method to layout the components.
Program Code:
import java.awt.*;
public SimpleCalculator() {
setTitle("Practical 3");
setLayout(new BorderLayout());
add(bp, BorderLayout.CENTER);
setSize(400, 500);
setVisible(true);
}
Output: