UNIT-I AWT Programs
UNIT-I AWT Programs
setBackground(Color.cyan);
setForeground(Color.red);
}
public static void main(String args[])
{
FrameDemo f1=new FrameDemo();
f1.setVisible(true);
f1.setSize(500,500);
f1.setTitle("My Frame");
}
}
//FlowLayout Manager
import java.awt.*;
class FlowLayoutDemo extends Frame
{
FlowLayoutDemo()
{
FlowLayout f1=new FlowLayout(FlowLayout.LEFT,20,20);
setLayout(f1);
Button b1=new Button("OK");
Button b2=new Button("CANCEL");
Button b3=new Button("RETRY");
add(b1);
add(b2);
add(b3);
}
public static void main(String args[])
{
FlowLayoutDemo n1=new FlowLayoutDemo();
n1.setVisible(true);
n1.setTitle("FlowLayoutDemo");
n1.setSize(500,500);
}
}
//FileDialog Class
import java.awt.*;
class FileDialogDemo extends Frame
{
public static void main(String args[])
{
FileDialogDemo f1=new FileDialogDemo();
f1.setVisible(true);
f1.setTitle("FileDialogDemo");
f1.setSize(500,500);
L1.setBounds(70,80,400,30);
L2.setBounds(70,150,200,30);
tf1.setBounds(280,150,150,30);
L3.setBounds(70,200,200,30);
tf2.setBounds(280,200,150,30);
b1.setBounds(125,300,200,30);
add(L1);
add(L2);add(tf1);
add(L3);add(tf2);
add(b1);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
int no=Integer.parseInt(tf1.getText());
if(no%2==0)
{
tf2.setText("Even No");
}
else
{
tf2.setText("Odd No");
}
}
public static void main(String args[])
{
CheckEvenOdd c1=new CheckEvenOdd();
c1.setVisible(true);
c1.setSize(500,400);
c1.setTitle("EVEN ODD DEMO");
}
}
//Checkboxgroup
import java.awt.*;
class CheckboxgroupDemo extends Frame
{
CheckboxgroupDemo()
{
FlowLayout f1=new FlowLayout();
setLayout(f1);
CheckboxGroup cbg=new CheckboxGroup();
Checkbox c1=new Checkbox("C Lang",false,cbg);
Checkbox c2=new Checkbox("C++ Lang",false,cbg);
Checkbox c3=new Checkbox("Java Lang",true,cbg);
add(c1);
add(c2);
add(c3);
}
public static void main(String args[])
{
CheckboxgroupDemo f1=new CheckboxgroupDemo();
f1.setVisible(true);
f1.setSize(500,400);
f1.setTitle("Checkbox Demo");
}
}
//Checkboxgroup
import java.awt.*;
import java.awt.event.*;
class CheckboxEventDemo extends Frame implements ItemListener
{
CheckboxGroup cbg;
Checkbox c1,c2,c3;
Label L1;
CheckboxEventDemo()
{
FlowLayout f1=new FlowLayout();
setLayout(f1);
cbg=new CheckboxGroup();
c1=new Checkbox("C Lang",false,cbg);
c2=new Checkbox("C++ Lang",false,cbg);
c3=new Checkbox("Java Lang",true,cbg);
L1=new Label("
");
add(c1);
add(c2);
add(c3);
add(L1);
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
Checkbox x=cbg.getSelectedCheckbox();
if(x==c1)
{
L1.setText("You have selected C Lang");
}
else if(x==c2)
{
L1.setText("You have selected C++ Lang");
}
else
{
L1.setText("You have selected Java Lang");
}
}
public static void main(String args[])
{
CheckboxEventDemo f1=new CheckboxEventDemo();
f1.setVisible(true);
f1.setSize(500,400);
f1.setTitle("Checkbox Demo");
}
}
//Checkbox
import java.awt.*;
class CheckboxDemo extends Frame
{
CheckboxDemo()
{
FlowLayout f1=new FlowLayout();
setLayout(f1);
Checkbox c1=new Checkbox();
Checkbox c2=new Checkbox("C Lang");
Checkbox c3=new Checkbox("C++ Lang",true);
add(c1);
add(c2);
add(c3);
}
public static void main(String args[])
{
CheckboxDemo f1=new CheckboxDemo();
f1.setVisible(true);
f1.setSize(500,400);
f1.setTitle("Checkbox Demo");
}
}
//CardLayout Manager
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class CardLayoutDemo extends JFrame implements ActionListener
{
CardLayout Card;
Container C;
Button b1,b2,b3,b4;
CardLayoutDemo()
{
Card=new CardLayout();
C=getContentPane();
C.setLayout(Card);
C.add(b1,"a");
C.add(b2,"b");
C.add(b3,"c");
C.add(b4,"d");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
Card.next(C);
}
public static void main(String args[])
{
CardLayoutDemo cld=new CardLayoutDemo();
cld.setVisible(true);
cld.setTitle("CardLayoutDemo");
cld.setSize(500,500);
}
}
//Calculator in java
//void setBounds(int x,int y,int width,int height);
import java.awt.*;
import java.awt.event.*;
class Calculator extends Frame implements ActionListener
{
Label L1,L2,L3,L4;
TextField tf1,tf2,tf3;
Button b1,b2,b3,b4;
Calculator()
{
setLayout(null);
setBackground(Color.cyan);
setForeground(Color.red);
Font f1=new Font("Arial Black",Font.BOLD,20);
setFont(f1);
L1=new Label("VJTECH CALCULATOR",Label.CENTER);
L2=new Label("Enter 1st Number:");
L3=new Label("Enter 2nd Number:");
L4=new Label("Result:");
tf1=new TextField(30);
tf2=new TextField(30);
tf3=new TextField(30);
b1=new Button("ADD");
b2=new Button("SUB");
b3=new Button("MUL");
b4=new Button("DIV");
L1.setBounds(50,80,400,30);
L2.setBounds(40,150,200,30);
tf1.setBounds(250,150,180,30);
L3.setBounds(40,230,200,30);
tf2.setBounds(250,230,180,30);
L4.setBounds(40,310,200,30);
tf3.setBounds(250,310,180,30);
b1.setBounds(40,410,100,40);
b2.setBounds(170,410,100,40);
b3.setBounds(300,410,100,40);
b4.setBounds(430,410,100,40);
add(L1);
add(L2);add(tf1);
add(L3);add(tf2);
add(L4);add(tf3);
add(b1);add(b2);add(b3);add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
int a=Integer.parseInt(tf1.getText());
int b=Integer.parseInt(tf2.getText());
int c;
if(b1==ae.getSource())
{
c=a+b;
tf3.setText(c+"");
}
else if(b2==ae.getSource())
{
c=a-b;
tf3.setText(c+"");
}
else if(b3==ae.getSource())
{
c=a*b;
tf3.setText(c+"");
}
else if(b4==ae.getSource())
{
float d=(float)a/b;
tf3.setText(d+"");
}
}
public static void main(String args[])
{
Calculator c1=new Calculator();
c1.setVisible(true);
c1.setTitle("Calculator");
c1.setSize(550,500);
}
}
//Button Class
import java.awt.*;
class ButtonDemo extends Frame
{
ButtonDemo()
{
FlowLayout f1=new FlowLayout();
setLayout(f1);
setBackground(Color.cyan);
Button b1=new Button("OK");
Button b2=new Button("Cancel");
Button b3=new Button("Retry");
add(b1);
add(b2);
add(b3);
}
public static void main(String args[])
{
ButtonDemo f1=new ButtonDemo();
f1.setVisible(true);
f1.setTitle("My Frame");
f1.setSize(500,500);
}
}
//BorderLayout Manager
import java.awt.*;
class BorderLayoutDemo extends Frame
{
BorderLayoutDemo()
{
BorderLayout g1=new BorderLayout();
setLayout(g1);
Button b1=new Button("EAST Region");
Button b2=new Button("WEST Region");
Button b3=new Button("SOUTH Region");
Button b4=new Button("NORTH Region");
TextArea ta1=new TextArea("This is center region");
add(b1,BorderLayout.EAST);
add(b2,BorderLayout.WEST);
add(b3,BorderLayout.SOUTH);
add(b4,BorderLayout.NORTH);
add(ta1,BorderLayout.CENTER);
}
public static void main(String args[])
{
BorderLayoutDemo n1=new BorderLayoutDemo();
n1.setVisible(true);
n1.setTitle("BorderLayoutDemo");
n1.setSize(500,500);
}
}
//Applet Program
import java.applet.*;
import java.awt.*;
public class AppletDemo extends Applet
{
public void init()
{
setBackground(Color.cyan);
setForeground(Color.red);
}
public void paint(Graphics g)
{
g.drawString("Welcome to World of Applet",150,150);
}
}
/*
<applet code="AppletDemo.class" width="500" height="500">
</applet>
*/
//Window Closing Program
import java.awt.*;
import java.awt.event.*;
class WindowCloseDemo extends Frame implements WindowListener
{
WindowCloseDemo()
{
addWindowListener(this);
}
public void windowClosing(WindowEvent we)
{
dispose();
}
public void windowDeactivated(WindowEvent we){}
public void windowActivated(WindowEvent we){}
public void windowDeiconified(WindowEvent we){}
public void windowIconified(WindowEvent we){}
public void windowClosed(WindowEvent we){}
public void windowOpened(WindowEvent we){}
public static void main(String args[])
{
WindowCloseDemo n1=new WindowCloseDemo();
n1.setVisible(true);
n1.setTitle("WindowCloseDemo");
n1.setSize(500,500);
}
}
//TextField Class
import java.awt.*;
class TextFieldDemo extends Frame
{
TextFieldDemo()
{
FlowLayout f1=new FlowLayout();
setLayout(f1);
Label L1=new Label("Enter User Name:");
Label L2=new Label("Enter Password:");
TextField tf1=new TextField("VJTech",30);
TextField tf2=new TextField(30);
Button b1=new Button("Login");
tf2.setEchoChar('*');
add(L1);add(tf1);
add(L2);add(tf2);
add(b1);
}
public static void main(String args[])
{
TextFieldDemo f1=new TextFieldDemo();
f1.setVisible(true);
f1.setTitle("My Frame");
f1.setSize(500,500);
}
}
import java.awt.*;
class TextAreaDemo extends Frame
{
TextAreaDemo()
{
FlowLayout f1=new FlowLayout();
setLayout(f1);
TextArea ta1=new TextArea("Write your
feedback",25,50,TextArea.SCROLLBARS_BOTH);
add(ta1);
}
public static void main(String args[])
{
TextAreaDemo f1=new TextAreaDemo();
f1.setVisible(true);
f1.setSize(500,500);
f1.setTitle("TextAreaDemo");
}
}
//Student Registration form
import java.awt.*;
import java.awt.event.*;
class StudentRegDemo extends Frame implements ActionListener
{
Label L7;
StudentRegDemo()
{
setLayout(null);
setBackground(Color.cyan);
setForeground(Color.red);
Label L=new Label("STUDENT REGISTRATION FORM");
Label L1=new Label("Enter Full Name:");
Label L2=new Label("Enter Mobile No:");
Label L3=new Label("Enter Address:");
Label L4=new Label("Enter Email ID:");
Label L5=new Label("Enter City Name:");
Label L6=new Label("Enter DOB:");
L7=new Label(" ");
TextField tf1=new TextField(30);
TextField tf2=new TextField(30);
TextField tf3=new TextField(30);
TextField tf4=new TextField(30);
TextField tf5=new TextField(30);
TextField tf6=new TextField(30);
Button b1=new Button("Submit");
L.setBounds(150,50,300,50);
L1.setBounds(50,130,150,30);
tf1.setBounds(220,130,200,30);
L2.setBounds(50,180,150,30);
tf2.setBounds(220,180,200,30);
L3.setBounds(50,230,150,30);
tf3.setBounds(220,230,200,30);
L4.setBounds(50,280,150,30);
tf4.setBounds(220,280,200,30);
L5.setBounds(50,330,150,30);
tf5.setBounds(220,330,200,30);
L6.setBounds(50,380,150,30);
tf6.setBounds(220,380,200,30);
b1.setBounds(180,450,150,30);
L7.setBounds(180,500,200,30);
add(L);
add(L1);add(tf1);
add(L2);add(tf2);
add(L3);add(tf3);
add(L4);add(tf4);
add(L5);add(tf5);
add(L6);add(tf6);
add(b1);add(L7);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
L7.setText("Records Submitted Successfully");
}
public static void main(String args[])
{
StudentRegDemo s1=new StudentRegDemo();
s1.setVisible(true);
s1.setTitle("Student Registration Form");
s1.setSize(500,600);
}
}
import java.awt.*;
class ScrollbarDemo extends Frame
{
ScrollbarDemo()
{
setLayout(null);
setBackground(Color.pink);
Scrollbar sb1=new Scrollbar(Scrollbar.VERTICAL,50,10,0,200);
Scrollbar sb2=new Scrollbar(Scrollbar.HORIZONTAL,25,10,0,100);
sb1.setBounds(450,40,50,420);
sb2.setBounds(10,450,420,50);
sb1.setBackground(Color.yellow);
sb2.setBackground(Color.yellow);
add(sb1);
add(sb2);
}
public static void main(String args[])
{
ScrollbarDemo f1=new ScrollbarDemo();
f1.setVisible(true);
f1.setSize(500,500);
f1.setTitle("ScrollbarDemo");
}
}
//Notepad in Java
import java.awt.*;
import java.awt.event.*;
class Notepad extends Frame implements ActionListener
{
MenuItem m12,m13;
Notepad()
{
setBackground(Color.pink);
mbr.add(m1);
mbr.add(m2);
mbr.add(m3);
mbr.add(m4);
setMenuBar(mbr);
m12.addActionListener(this);
m13.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==m12)
{
FileDialog fd=new
FileDialog(this,"Open",FileDialog.LOAD);
fd.setVisible(true);
}
if(ae.getSource()==m13)
{
FileDialog fd=new
FileDialog(this,"Save",FileDialog.SAVE);
fd.setVisible(true);
}
}
public static void main(String args[])
{
Notepad n1=new Notepad();
n1.setVisible(true);
n1.setTitle("Notepad");
n1.setSize(500,500);
}
}
import java.awt.*;