Pr
Pr
Practical No. 1
Aim :- Write a program to demonstrate the use of components like Label , TextField ,
TextArea , Button , Checkbox , RadioButton ( CheckboxGroup ) .
Program :-
import java.awt.*;
import javax.swing.*;
public class Demo
{
public static void main(String args[])
{
Frame f = new Frame("Addmission Form");
f.setBackground(Color.BLACK);
Label l = new Label("Addmission Form",Label.CENTER);
l.setForeground(Color.orange);
l.setFont(new Font("Serif",Font.BOLD,25));
l.setBounds(280,40,300,45);
f.add(l);
Label l1 = new Label("Name ");
l1.setForeground(Color.WHITE);
l1.setFont(new Font("Serif",Font.BOLD,20));
l1.setBounds(100,90,200,30);
f.add(l1);
TextField tf1 = new TextField();
tf1.setBounds(300,90,400,30);
tf1.setFont(new Font("Serif",Font.PLAIN,17));
f.add(tf1);
Label l2 = new Label("Branch ");
l2.setForeground(Color.WHITE);
l2.setFont(new Font("Serif",Font.BOLD,20));
l2.setBounds(100,140,200,30);
f.add(l2);
Choice ch1 = new Choice();
ch1.add("Computer");
ch1.add("Chemical");
ch1.add("Civil");
ch1.add("Electrical");
ch1.add("Electronic");
ch1.add("Mechanical");
ch1.setBounds(300,140,400,30);
ch1.setFont(new Font("Serif",Font.PLAIN,17));
f.add(ch1);
Label l3 = new Label("Mobile No ");
l3.setForeground(Color.WHITE);
l3.setFont(new Font("Serif",Font.BOLD,20));
l3.setBounds(100,190,200,30);
f.add(l3);
TextField tf2 = new TextField();
tf2.setBounds(300,190,400,30);
tf2.setFont(new Font("Serif",Font.PLAIN,17));
f.add(tf2);
Label l4 = new Label("Email Id ");
l4.setBounds(100,240,200,30);
l4.setFont(new Font("Serif",Font.BOLD,20));
l4.setForeground(Color.WHITE);
f.add(l4);
TextField tf3 = new TextField();
tf3.setBounds(300,240,400,30);
tf3.setFont(new Font("Serif",Font.PLAIN,17));
f.add(tf3);
Label l5 = new Label("Gender ");
l5.setBounds(100,290,200,30);
l5.setFont(new Font("Serif",Font.BOLD,20));
l5.setForeground(Color.WHITE);
f.add(l5);
CheckboxGroup cbg = new CheckboxGroup();
Checkbox cb1 = new Checkbox("Male",true,cbg);
Checkbox cb2 = new Checkbox("Female",false,cbg);
Checkbox cb3 = new Checkbox("Other",false,cbg);
cb1.setForeground(Color.WHITE);
cb2.setForeground(Color.WHITE);
cb3.setForeground(Color.WHITE);
cb1.setFont(new Font("Serif",Font.BOLD,18));
cb2.setFont(new Font("Serif",Font.BOLD,18));
cb3.setFont(new Font("Serif",Font.BOLD,18));
cb1.setBounds(300,290,60,30);
cb2.setBounds(400,290,90,30);
cb3.setBounds(500,290,120,30);
f.add(cb1);
f.add(cb2);
f.add(cb3);
Label l6 = new Label("Semester ");
l6.setBounds(100,340,200,30);
l6.setFont(new Font("Serif",Font.BOLD,20));
l6.setForeground(Color.WHITE);
f.add(l6);
Choice ch2 = new Choice();
ch2.add("First Semester");
ch2.add("Second Semester");
ch2.add("Third Semester");
ch2.add("Fourth Semester");
ch2.add("Fifth Semester");
ch2.add("Sixth Semester");
ch2.setBounds(300,340,400,30);
ch2.setFont(new Font("Serif",Font.PLAIN,17));
f.add(ch2);
Label l7 = new Label("Enrollment No ");
l7.setBounds(100,390,200,30);
l7.setFont(new Font("Serif",Font.BOLD,20));
l7.setForeground(Color.WHITE);
f.add(l7);
TextField tf4 = new TextField();
tf4.setBounds(300,390,400,30);
tf4.setFont(new Font("Serif",Font.PLAIN,17));
f.add(tf4);
Label l8 = new Label("Address ");
l8.setBounds(100,440,200,30);
l8.setFont(new Font("Serif",Font.BOLD,20));
l8.setForeground(Color.WHITE);
f.add(l8);
TextArea ta1 = new TextArea();
ta1.setBounds(300,440,400,50);
ta1.setFont(new Font("Serif",Font.PLAIN,17));
f.add(ta1);
Label l9 = new Label("Password ");
l9.setBounds(100,510,200,30);
l9.setFont(new Font("Serif",Font.BOLD,20));
l9.setForeground(Color.WHITE);
f.add(l9);
TextField tf5 = new TextField(10);
tf5.setBounds(300,510,400,30);
tf5.setFont(new Font("Serif",Font.PLAIN,17));
tf5.setEchoChar('*');
f.add(tf5);
Checkbox c1 = new Checkbox("Conform Password");
c1.setBounds(150,560,190,30);
c1.setForeground(Color.WHITE);
c1.setFont(new Font("Serif",Font.BOLD,14));
f.add(c1);
JButton b1 = new JButton("Submit");
b1.setForeground(Color.BLUE);
b1.setFont(new Font("SansSerif",Font.PLAIN,15));
b1.setBounds(350,610,100,30);
f.add(b1);
JButton b2 = new JButton("Cancel");
b2.setForeground(Color.BLUE);
b2.setFont(new Font("Serif",Font.PLAIN,15));
b2.setBounds(500,610,100,30);
f.add(b2);
f.setSize(1000,600);
f.setLayout(null);
f.setVisible(true);
}
}
Output : -
Conclusion : -
In above code , creates a graphical admission form with a black background and
orange text. It includes a title, and various input fields: name, branch , mobile number, email,
and semester . It also has a text area for the address and a password field with masked input.
Users can select their gender from radio buttons and confirm their password with a
checkbox. The form features "Submit" and "Cancel" buttons to submit the form or to cancel
it .
Program :-
import java.awt.*;
import java.awt.event.*;
public class FastFoodAndSweetsSelector {
public static void main(String[] args) {
Frame frame = new Frame("Fast Food and Sweets Selector");
frame.add(label);
frame.add(new Label("Fast Foods:"));
frame.add(fastFoodList);
frame.add(new Label("Sweets:"));
frame.add(sweetChoice);
frame.add(textArea);
frame.setSize(400, 300);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
Output:-
Conclusion :-
I successfully perform a code of the list and choice component in awt.
Aim:- Write a program to demonstrate the use of Border Layout showing four buttons
at four sides of an applet with caption “ left “ , “ right “ , “ top “ and “bottom “ .
Program :-
import java.awt.*;
public class Border
{
public static void main(String args[])
{
Frame f = new Frame("Border Layout");
Button b1 = new Button("Top");
Button b2 = new Button("Bottom");
Button b3 = new Button("Right");
Button b4 = new Button("Left");
f.add(b1,BorderLayout.NORTH);
f.add(b2,BorderLayout.SOUTH);
f.add(b3,BorderLayout.EAST);
f.add(b4,BorderLayout.WEST);
f.setSize(400,400);
f.setVisible(true);
}
}
Output :-
Conclusion :-
In above code creates a Frame with a BorderLayout that places four buttons at the top,
bottom, left, and right of the window.
Aim:- Write a program to design a simple calculator to demonstrate the use of Grid
Layout.
Program :-
import java.awt.*;
public class Gridy
{
public static void main(String args[])
{
Frame f = new Frame("GridLayout");
Button b1 = new Button("AC");
Button b2 = new Button("+/-");
Button b3 = new Button("%");
Button b4 = new Button("/");
Button b5 = new Button("7");
Button b6 = new Button("8");
Button b7 = new Button("9");
Button b8 = new Button("*");
Button b9 = new Button("4");
Button b10 = new Button("5");
Button b11 = new Button("6");
Button b12 = new Button("-");
Button b13 = new Button("1");
Button b14 = new Button("2");
Button b15 = new Button("3");
Button b16 = new Button("+");
Button b17 = new Button("0");
Button b18 = new Button(".");
Button b19 = new Button("=");
Button b20 = new Button("()");
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.add(b6);
f.add(b7);
f.add(b8);
f.add(b9);
f.add(b10);
f.add(b11);
f.add(b12);
f.add(b13);
f.add(b14);
f.add(b15);
f.add(b16);
f.add(b17);
f.add(b18);
f.add(b19);
f.add(b20);
f.setSize(400,400);
f.setLayout(new GridLayout(5,3));
f.setVisible(true);
}
}
Output :-
Conclusion :-
I successfully create a calculator using awt component.