Experiment 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

EXPERIMENT 1

AIM: Write A Program to Demonstrate the Use of following AWT Components.

 Radio Button and Checkbox


 Text Field, Text Area, Button and Label
THEORY:
AWT stands for Abstract window toolkit is an Application programming interface (API) for creating
Graphical User Interface (GUI) in Java. AWT provides various components like button, label,
checkbox, etc. used as objects inside a Java Program. AWT components use the resources of the
operating system, i.e., they are platform-dependent, which means, component's view can be changed
according to the view of the operating system. The classes for AWT are provided by the Java.awt
package for various AWT components.

 Components: AWT provides various components such as buttons, labels, text fields,
checkboxes, etc used for creating GUI elements for Java Applications.
 Containers: AWT provides containers like panels, frames, and dialogues to organize and
group components in the Application.
 Layout Managers: Layout Managers are responsible for arranging data in the containers sone
of the layout managers are BorderLayout, FlowLayout, etc.
 Event Handling: AWT allows the user to handle the events like mouse clicks, key presses,
etc. using event listeners and adapters.
 Graphics and Drawing: It is the feature of AWT that helps to draw shapes, insert images and
write text in the components of a Java Application.

import java.awt.*;
public class AwtApp extends Frame {
AwtApp(){
Label firstName = new Label("First Name");
firstName.setBounds(20, 50, 80, 20);

Label lastName = new Label("Last Name");


lastName.setBounds(20, 80, 80, 20);

Label dob = new Label("Date of Birth");


dob.setBounds(20, 110, 80, 20);

TextField firstNameTF = new TextField();


firstNameTF.setBounds(120, 50, 100, 20);

TextField lastNameTF = new TextField();


lastNameTF.setBounds(120, 80, 100, 20);

TextField dobTF = new TextField();


dobTF.setBounds(120, 110, 100, 20);

Button sbmt = new Button("Submit");


sbmt.setBounds(20, 160, 100, 30);

Button reset = new Button("Reset");


reset.setBounds(120,160,100,30);

add(firstName);
add(lastName);
add(dob);
add(firstNameTF);
add(lastNameTF);
add(dobTF);
add(sbmt);
add(reset);

setSize(300,300);
setLayout(null);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
AwtApp awt = new AwtApp();
}
}

CODE 2:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/*<applet code="swing.class" height=500 width=500></applet>*/
public class swing extends JApplet implements ActionListener
{
JTextField tf;
JLabel l1,l2,l3,l4,l5;
JButton b;
JComboBox cb;
JComboBox cb1;
JList jl;
JRadioButton m,f;
ButtonGroup bg;
JPanel p;
public void init()
{
Color c=new Color(215,248,90);
p=new JPanel();
p.setBackground(c);
l1=new JLabel("Enter Your Name:");
l2=new JLabel("Gender:");
l3=new JLabel("Qualification:");
l4=new JLabel("Hobbies:");
l5=new JLabel("Year:");
tf=new JTextField(20);
b=new JButton("Show");
String str[]={"BA","BBA","BCA","B.TECH","MBA","MA","MCA"};
String str1[]={"Watching TV","Reading BOOKS","Watching MOVIES",
"Playing VIDEO GAMES","Surfing INTERNET"};
String str2[]={"1st Year","IInd Year","IIIrd Year","IVth Year"};
GridBagLayout gl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
p.setLayout(gl);
cb=new JComboBox(str);
jl=new JList(str1);
cb1=new JComboBox(str2);
m=new JRadioButton("Male");
f=new JRadioButton("Female");
gbc.gridx=0;
gbc.gridy=0;
gbc.anchor=GridBagConstraints.CENTER;
gbc.fill=GridBagConstraints.HORIZONTAL;
gl.setConstraints(l1,gbc);
p.add(l1);
gbc.gridx=1;
gbc.gridy=0;
gbc.gridwidth=2;
gbc.anchor=GridBagConstraints.CENTER;
gl.setConstraints(tf,gbc);
p.add(tf);
gbc.gridx=0;
gbc.gridy=2;
gbc.gridwidth=1;
gbc.anchor=GridBagConstraints.CENTER;
gl.setConstraints(l2,gbc);
p.add(l2);
bg=new ButtonGroup();
gbc.gridx=1;
gbc.gridy=2;
gbc.anchor=GridBagConstraints.CENTER;

You might also like