Tuesday, May 12, 2020

Write a Java Program to Login Form to Connect Database validate username and password

Database Connecting Program

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class dbConnect {

        Connection conn = null;
        
        String host = "jdbc:mysql://localhost:3306/";
        String dbName = "cse1";
        String username = "root";
        String password = "vamsi";
        String driver = "com.mysql.jdbc.Driver";
        
        Connection con;
        PreparedStatement pst;
        ResultSet rs;
        
        dbConnect()
        {
                try{
                        Class.forName(driver);
                        con = DriverManager.getConnection(host + dbName, username, password);
                        pst = con.prepareStatement("SELECT * FROM login WHERE uname = ? and pwd = ?");
                }
                catch(Exception e)
                {
                        e.printStackTrace();    
                }
        }
        
        public Boolean vertify(String uname, String pwd)
        {
                try{
                        pst.setString(1, uname); //this replace the 1st "?" in the query for username
                        pst.setString(2, pwd);   //this replace the 2nd "?" in the query for password
                        
                        //execute the prepared statement
                        rs = pst.executeQuery();
                        if(rs.next())
                        {
                                //TRUE iff query founds any corresponding data
                                return true;
                        }
                        else
                        {
                                return false;
                        }                       
                }catch (Exception e)
                {
                        //TODO Auto-generate catch block
                        System.out.println("error while validating" + e);
                        return false;
                }
        }
}
_______________________________________________________________________________
Graphical User Interface

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class logInMenu1{
        Frame frame1 = new Frame("Log-in Form");
        
        Button SUBMIT;
        
        Label lUsername, lPassword;
        TextField username, password;
        
        dbConnect dbC=new dbConnect();
        
        public logInMenu1()
        {
                createAndShowGUI();
        }
        
        void login()
        {
                lUsername = new Label("Username");
                username = new TextField(15);
                
                lPassword = new Label("Password");
                password = new TextField(15);
                
                SUBMIT = new Button("SUBMIT");
                
                Panel panel = new Panel(new GridLayout(3,1));
                panel.setSize(200,50);
                panel.add(lUsername);
                panel.add(username);
                panel.add(lPassword);
                panel.add(password);
                panel.add(SUBMIT);
                frame1.add(panel, BorderLayout.CENTER);

                SUBMIT.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent e)
                        {
                                
                                String uname = username.getText();

                                String pwd = password.getText();
                                
                                System.out.println("value1: " + uname);
                                System.out.println("value2: " + pwd);
                                
                                if(dbC.vertify(uname, pwd))
                                {
                                        //a pop-up box
                                        JOptionPane.showMessageDialog(null, "You have logged in successfully", "Success",
                                                        JOptionPane.INFORMATION_MESSAGE);
                                }
                                else
                                {
                                        //a pop-up box
                                        JOptionPane.showMessageDialog(null, "Login failed!", "Failed!",
                                                        JOptionPane.INFORMATION_MESSAGE);
                                }
                        }
                });
        }
        
        void createAndShowGUI(){
                 frame1.setSize(400, 150);
                frame1.setVisible(true);
                frame1.addWindowListener(new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                });
                login();
        }
        
        public static void main(String[] args)
        {
          new logInMenu1();
        }

}

write a java program to store data emp table

import java.sql.*;  
class MysqlCon1{  
public static void main(String args[]){  
try{  
Class.forName("com.mysql.jdbc.Driver");  
Connection con=DriverManager.getConnection(  
"jdbc:mysql://localhost:3306/cse1","root","");  
Statement stmt=con.createStatement();  
int result=stmt.executeUpdate("insert into emp values(122,'raghu')");  
System.out.println(result+" records affected");   
con.close();  
}catch(Exception e){ System.out.println(e);}  
}  

write a java program to read data from the emp database display details

import java.sql.*;  
class MysqlCon{  
public static void main(String args[]){  
try{  
Class.forName("com.mysql.jdbc.Driver");  
Connection con=DriverManager.getConnection(  
"jdbc:mysql://localhost:3306/cse1","root","vamsi");  
//here sonoo is database name, root is username and password  
Statement stmt=con.createStatement();  
ResultSet rs=stmt.executeQuery("select * from emp");  
while(rs.next())  
System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));  
con.close();  
}catch(Exception e){ System.out.println(e);}  
}  

write jdbc program to test java connection

import java.sql.*;
public class TestJdbc {
    public static void main(String[] args) {
        Connection conn = null;

        String url = "jdbc:mysql://localhost:3306/";        
        String driver = "com.mysql.jdbc.Driver";

        String dbName = "cse1", userName = "root", 
            password = "vamsi";      

        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url+dbName, userName, password);
            System.out.println("Connected to the database");
            conn.close();
            System.out.println("Disconnected from database");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Monday, May 11, 2020

write java program using Grid Bag Layout

import java.awt.Button;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
public class AWTGridBagLayoutDemo extends JFrame {
public static void main(String[] args) {
AWTGridBagLayoutDemo gbld = new AWTGridBagLayoutDemo();
}
public AWTGridBagLayoutDemo() {
setSize(300, 300);
setPreferredSize(getSize());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbcnt = new GridBagConstraints();
setLayout(gbl);
setTitle("Java Layout Manager: GridBag Layout");
GridBagLayout layout = new GridBagLayout(); //create grid bag layout
this.setLayout(layout);
//below code is customization of grid fashion
gbcnt.fill = GridBagConstraints.HORIZONTAL;
gbcnt.gridx = 1;
gbcnt.gridy = 0;
this.add(new Button("Smart Phone"), gbcnt);
gbcnt.gridx = 2;
gbcnt.gridy = 0;
this.add(new Button("Laptop"), gbcnt);
gbcnt.fill = GridBagConstraints.HORIZONTAL;
gbcnt.ipady = 20;
gbcnt.gridx = 1;
gbcnt.gridy = 1;
this.add(new Button("Tablet"), gbcnt);
gbcnt.gridx = 2;
gbcnt.gridy = 1;
this.add(new Button("Desktop"), gbcnt);
gbcnt.gridx = 1;
gbcnt.gridy = 2;
gbcnt.fill = GridBagConstraints.HORIZONTAL;
gbcnt.gridwidth = 2;
this.add(new Button("Computer"), gbcnt);
}
}

write a java program to create calculator for addition,subtraction,multiplication,division operations

import java.awt.*;
import java.awt.event.*;
 
class Calculator implements ActionListener
{
//Declaring Objects
Frame f=new Frame();
Label l1=new Label("First Number");
Label l2=new Label("Second Number");
Label l3=new Label("Result");
TextField t1=new TextField();
TextField t2=new TextField();
TextField t3=new TextField();
Button b1=new Button("Add");
Button b2=new Button("Sub");
Button b3=new Button("Mul");
Button b4=new Button("Div");
Button b5=new Button("Cancel");
Calculator()
{
//Giving Coordinates
l1.setBounds(50,100,100,20);
l2.setBounds(50,140,100,20);
l3.setBounds(50,180,100,20);
t1.setBounds(200,100,100,20);
t2.setBounds(200,140,100,20);
t3.setBounds(200,180,100,20);
b1.setBounds(50,250,50,20);
b2.setBounds(110,250,50,20);
b3.setBounds(170,250,50,20);
b4.setBounds(230,250,50,20);
b5.setBounds(290,250,50,20);
//Adding components to the frame
f.add(l1);
f.add(l2);
f.add(l3);
f.add(t1);
f.add(t2);
f.add(t3);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
f.setLayout(null);
f.setVisible(true);
f.setSize(400,350);
f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            } });
}
public void actionPerformed(ActionEvent e)
{
int n1=Integer.parseInt(t1.getText());
int n2=Integer.parseInt(t2.getText());
if(e.getSource()==b1)
{
t3.setText(String.valueOf(n1+n2));
}
if(e.getSource()==b2)
{
t3.setText(String.valueOf(n1-n2));
}
if(e.getSource()==b3)
{
t3.setText(String.valueOf(n1*n2));
}
if(e.getSource()==b4)
{
t3.setText(String.valueOf(n1/n2));
}
if(e.getSource()==b5)
{
System.exit(0);
}

}
public static void main(String...s)
{
new Calculator();
}
}






Sunday, May 10, 2020

write a java program to demonstrate Font Metrics Class

import java.awt.*;
/* <APPLET CODE ="FontMetricsClass.class" WIDTH=300 HEIGHT=200> </APPLET> */
public class FontMetricsClass extends java.applet.Applet
  {
       public void paint(Graphics g)
            {
                String s="Hello Java";
                Font f=new Font("Arial",Font.BOLD+Font.ITALIC,20);
                g.setFont(f);
                FontMetrics met=g.getFontMetrics(f);
                int ascent=met.getAscent();
                int height=met.getHeight();
                int leading=met.getLeading();
                int baseline=leading+ascent;
                for(int i=0;i<10;i++)
                   {
                       g.drawString("Line"+String.valueOf(i),10,baseline);
                       baseline+=height;
                   }
           }
  }    
   

write a java program to create Dialog using awt

import java.awt.*;  
import java.awt.event.*;  
public class DialogExample {  
    private static Dialog d;  
    DialogExample() {  
        Frame f= new Frame();  
        d = new Dialog(f , "Dialog Example", true);  
        d.setLayout( new FlowLayout() );  
        Button b = new Button ("OK");  
        b.addActionListener ( new ActionListener()  
        {  
            public void actionPerformed( ActionEvent e )  
            {  
                DialogExample.d.setVisible(false);  
            }  
        });  
        d.add( new Label ("Click button to continue."));  
        d.add(b);  
        d.setSize(300,300);    
        d.setVisible(true);  
d.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            } });
    }  
    public static void main(String args[])  
    {  
        new DialogExample();  
    }  
}  

write a java program to create popupmenu

import java.awt.*;  
import java.awt.event.*;  
class PopupMenuExample  
{  
     PopupMenuExample(){  
      final  Frame f= new Frame("PopupMenu Example");  
        final PopupMenu popupmenu = new PopupMenu("Edit");   
         MenuItem cut = new MenuItem("Cut");  
         cut.setActionCommand("Cut");  
         MenuItem copy = new MenuItem("Copy");  
         copy.setActionCommand("Copy");  
         MenuItem paste = new MenuItem("Paste");  
         paste.setActionCommand("Paste");      
         popupmenu.add(cut);  
         popupmenu.add(copy);  
         popupmenu.add(paste);        
         f.addMouseListener(new MouseAdapter() {  
            public void mouseClicked(MouseEvent e) {              
                popupmenu.show(f , e.getX(), e.getY());  
            }                 
         });  
         f.add(popupmenu);   
         f.setSize(400,400);  
         f.setLayout(null);  
         f.setVisible(true);  
f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            } });
     }  
public static void main(String args[])  
{  
        new PopupMenuExample();  
}  
}  

write a java program for creating Menu using awt

import java.awt.*;  
import java.awt.event.*;
class MenuExample  
{  
     MenuExample(){  
         Frame f= new Frame("Menu and MenuItem Example");  
         MenuBar mb=new MenuBar();  
         Menu menu=new Menu("Menu");  
         Menu submenu=new Menu("Sub Menu");  
         MenuItem i1=new MenuItem("Item 1");  
         MenuItem i2=new MenuItem("Item 2");  
         MenuItem i3=new MenuItem("Item 3");  
         MenuItem i4=new MenuItem("Item 4");  
         MenuItem i5=new MenuItem("Item 5");  
         menu.add(i1);  
         menu.add(i2);  
         menu.add(i3);  
         submenu.add(i4);  
         submenu.add(i5);  
         menu.add(submenu);  
         mb.add(menu);  
         f.setMenuBar(mb);  
         f.setSize(400,400);  
         f.setLayout(null);  
         f.setVisible(true);  
f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            } });
}  
public static void main(String args[])  
{  
new MenuExample();  
}  
}  

Wednesday, May 6, 2020

write a java program to handle Mouse Events Using Mouse Adapter

import java.awt.*;  
import java.awt.event.*;  
public class MouseAdapterExample extends MouseAdapter{  
    Frame f;  
    MouseAdapterExample(){  
        f=new Frame("Mouse Adapter");  
        f.addMouseListener(this);  
          
        f.setSize(300,300);  
        f.setLayout(null);  
        f.setVisible(true);  
f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            } });
    }  
    public void mouseClicked(MouseEvent e) {  
        Graphics g=f.getGraphics();  
        g.setColor(Color.BLUE);  
        g.fillOval(e.getX(),e.getY(),30,30);  
    }  
      
public static void main(String[] args) {  
    new MouseAdapterExample();  
}  
}  

write a java program to handle KeyEvents using KeyAdapter

import java.awt.*;  
import java.awt.event.*;  
public class KeyAdapterExample extends KeyAdapter{  
    Label l;  
    TextArea area;  
    Frame f;  
    KeyAdapterExample(){  
        f=new Frame("Key Adapter");  
        l=new Label();  
        l.setBounds(20,50,200,20);  
        area=new TextArea();  
        area.setBounds(20,80,300, 300);  
        area.addKeyListener(this);  
          
        f.add(l);f.add(area);  
        f.setSize(400,400);  
        f.setLayout(null);  
        f.setVisible(true);  
f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            } });
    }  
    public void keyReleased(KeyEvent e) {  
        l.setText("Key Released");  
    }  
  
    public static void main(String[] args) {  
        new KeyAdapterExample();  
    }  
}  

write a java program to handle mouse events using Mouse Listener

import java.awt.*;  
import java.awt.event.*;  
public class MouseListenerExample extends Frame implements MouseListener{  
    Label l;  
    MouseListenerExample(){  
        addMouseListener(this);  
          
        l=new Label();  
        l.setBounds(20,50,100,20);  
        add(l);  
        setSize(300,300);  
        setLayout(null);  
        setVisible(true); 
addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            } });
    }  
    public void mouseClicked(MouseEvent e) {  
        l.setText("Mouse Clicked");  
    }  
    public void mouseEntered(MouseEvent e) {  
        l.setText("Mouse Entered");  
    }  
    public void mouseExited(MouseEvent e) {  
        l.setText("Mouse Exited");  
    }  
    public void mousePressed(MouseEvent e) {  
        l.setText("Mouse Pressed");  
    }  
    public void mouseReleased(MouseEvent e) {  
        l.setText("Mouse Released");  
    }  
public static void main(String[] args) {  
    new MouseListenerExample();  
}  
}  

write a java program to handle key press events using Key Listener

import java.awt.*;  
import java.awt.event.*;  
public class KeyListenerExample extends Frame implements KeyListener{  
    Label l;  
    TextArea area;  
    KeyListenerExample(){  
          
        l=new Label();  
        l.setBounds(20,50,100,20);  
        area=new TextArea();  
        area.setBounds(20,80,300, 300);  
        area.addKeyListener(this);  
        add(l);
add(area);  
        setSize(400,400);  
        setLayout(null);  
        setVisible(true);  
addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            } });
    }  
    public void keyPressed(KeyEvent e) {  
        l.setText("Key Pressed");  
    }  
 public void keyReleased(KeyEvent e) {  
   l.setText("Key Released");  
   }  
    public void keyTyped(KeyEvent e) {  
        l.setText("Key Typed");  
    }  
  
    public static void main(String[] args) {  
        new KeyListenerExample();  
    }  
}  

write a java program to handle Text Field and Button using Action Listener

import java.awt.*;
import java.awt.event.*;
class EventHandle extends Frame implements ActionListener{
TextField textField;
EventHandle()
{
textField = new TextField();
textField.setBounds(60,50,170,20);
Button button = new Button("Quote");
button.setBounds(90,140,75,40);
button.addActionListener(this);
add(button);
add(textField);
setSize(250,250);
setLayout(null);
setVisible(true);
 addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            } });
}
public void actionPerformed(ActionEvent e){
textField.setText("Keep Learning");
}
public static void main(String args[]){
new EventHandle();
}  }


Monday, May 4, 2020

write a java program to create multiple threads

class NewThread implements Runnable {
  String name; // name of thread

  Thread t;

  NewThread(String threadname) {
    name = threadname;
    t = new Thread(this, name);
    System.out.println("New thread: " + t);
    t.start(); // Start the thread
  }

  public void run() {
    try {
      for (int i = 5; i > 0; i--) {
        System.out.println(name + ": " + i);
        Thread.sleep(1000);
      }
    } catch (InterruptedException e) {
      System.out.println(name + "Interrupted");
    }
    System.out.println(name + " exiting.");
  }
}

class MultiThreadDemo {
  public static void main(String args[]) {
    new NewThread("One"); // start threads
    new NewThread("Two");
    new NewThread("Three");

    try {
      Thread.sleep(10000);
    } catch (InterruptedException e) {
      System.out.println("Main thread Interrupted");
    }

    System.out.println("Main thread exiting.");
  }
}

Write java program that creates three threads. The first thread displays "Good Morning" for every one second, the second thread displays "Hello" for every two seconds and third thread displays "Welcome" for every three seconds.

class A extends Thread
{
     public void run()
     {
    try
    {
        while(true)
        {
           sleep(1000);
           System.out.println("good morning");
        }
    }
    catch(Exception e)
    { }
      }
}
class B extends Thread
{
      public void run()
      {
    try
    {
        while(true)
        {
        sleep(2000);
        System.out.println("hello");
        }
        }
      catch(Exception e)
    { }
      }
}
class C extends Thread
{
    public void run()
     {
    try
    {
        while(true)
        {
            sleep(3000);
            System.out.println("welcome");
        }
    }
    catch(Exception e)
    { }
     }
}
class ThreadDemo13
{
    public static void main(String args[])
    {
        A t1=new A();
        B t2=new B();
        C t3=new C();
        t1.start();
        t2.start();
        t3.start();
    }
}

Synchronization Method - Multi Threading

class First
{
 synchronized void display(String msg)
 {
  System.out.print ("["+msg);
  try
  {
   Thread.sleep(1000);
  }
  catch(InterruptedException e)
  {
   e.printStackTrace();
  }
  System.out.println ("]");
 }
}

class Second extends Thread
{
 String msg;
 First fobj;
 Second (First fp,String str)
 {
  fobj = fp;
  msg = str;
  start();
 }
 public void run()
 {
  fobj.display(msg);
 }
}

public class Syncro1
{
 public static void main (String[] args)
 {
  First fnew = new First();
  Second ss = new Second(fnew, "welcome");
  Second ss1= new Second (fnew,"new");
  Second ss2 = new Second(fnew, "programmer");
 }
}

Synchronization Block - Multi Threading Program in java

class First
{
 public void display(String msg)
 {
  System.out.print ("["+msg);
  try
  {
   Thread.sleep(1000);
  }
  catch(InterruptedException e)
  {
   e.printStackTrace();
  }
  System.out.println ("]");
 }
}

class Second extends Thread
{
 String msg; 
 First fobj;
 Second (First fp,String str)
 {
  fobj = fp;
  msg = str;
  start();
 }
 public void run()
 {
  synchronized(fobj)       //Synchronized block
  {
   fobj.display(msg);
  }
 }
}

public class Syncro2
{
 public static void main (String[] args)
 {
  First fnew = new First();
  Second ss = new Second(fnew, "welcome");
  Second ss1= new Second (fnew,"new");
  Second ss2 = new Second(fnew, "programmer");
 }
}

Grid Bag Layout - Rowspan- Colspan- Layout Manager

import java.awt.*;
/* <applet code=GridBag3.class width=300 height=300></applet>*/
public class GridBag3 extends java.applet.Applet {
    GridBagConstraints constraints = new GridBagConstraints();
    void addGB( Component component, int x, int y  ) {
        constraints.gridx = x; 
        constraints.gridy = y;
        add ( component, constraints );
    }
public void init() {
        setLayout( new GridBagLayout() );
        constraints.weightx = 1.0;
        constraints.weighty = 1.0;
        constraints.fill = GridBagConstraints.BOTH;
        int x, y;  // for clarity
        constraints.gridheight = 2; // Span two rows
        addGB( new Button("one"),   x=0, y=0 );
        constraints.gridheight = 1; // set it back
        addGB( new Button("two"),   x=1, y=0 );
        addGB( new Button("three"), x=2, y=0 );
        constraints.gridwidth = 2; // Span two columns
        addGB( new Button("four"),  x=1, y=1 );
        constraints.gridwidth = 1; // set it back
    }
}

Grid Bag Layout- Increasing Cell Size - Layout Manager

import java.awt.*;
/* <applet code=GridBag2.class width=300 height=300></applet>*/
public class GridBag2 extends java.applet.Applet {
    GridBagConstraints constraints = new GridBagConstraints();
    void addGB( Component component, int x, int y  ) {
        constraints.gridx = x; 
        constraints.gridy = y;
        add ( component, constraints );
    }
 public void init() {
        setLayout( new GridBagLayout() );
        constraints.weightx = 1.0;
        constraints.weighty = 1.0;
        constraints.fill = GridBagConstraints.BOTH;
        int x, y;  // for clarity
        addGB( new Button("North"),  x=1,y=0 );
        addGB( new Button("West"),   x=0,y=1 );
        addGB( new Button("Center"), x=1,y=1 );
        addGB( new Button("East"),   x=2,y=1 );
        addGB( new Button("North"),  x=1,y=2 );
    }
}

Grid Bag Layout - Normal - Layout Manager

import java.awt.*;
/* <applet code=GridBag1.class width=300 height=300></applet>*/
public class GridBag1 extends java.applet.Applet {
    GridBagConstraints constraints = new GridBagConstraints();
    void addGB( Component component, int x, int y  ) {
        constraints.gridx = x; 
        constraints.gridy = y;
        add ( component, constraints );
    }
   
    public void init() {
        setLayout( new GridBagLayout() );
        int x, y;  // for clarity
        addGB( new Button("North"),  x=1,y=0 );
        addGB( new Button("West"),   x=0,y=1 );
        addGB( new Button("Center"), x=1,y=1 );
        addGB( new Button("East"),   x=2,y=1 );
        addGB( new Button("South"),  x=1,y=2 );
    }
}

Card Layout - Layout Manager

import java.awt.*;
    import java.applet.Applet; 
/* <applet code=Test5.class width=300 height=300></applet>*/
public class Test5 extends java.applet.Applet {
    CardLayout cards = new CardLayout();
     
    public void init() {
        setLayout( cards );
        add( new Button("one"), "one" );
        add( new Button("two"), "two" );
        add( new Button("three"), "three" );
    }
     
    public boolean action( Event e, Object arg) {
        cards.next( this );
        return true;
    }
}

Border Layout - Layout Managers

import java.awt.*;
import java.awt.event.*;
class  Border extends Frame
{
Border(String s)
{
super(s);
setSize(300,140);
setLayout(new BorderLayout());

add(new Button("North"),BorderLayout.NORTH);
add(new Button("East"),BorderLayout.EAST);
add(new Button("South"),BorderLayout.SOUTH);
add(new Button("West"),BorderLayout.WEST);
        add(new Button("Center"),BorderLayout.CENTER);
setVisible(true);
setSize(300,300);
addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            } });

} }
class Test4
{
public static void main(String[] args)
{
Border  f= new Border("Border Layout");
} }

Grid Layout Program - Layout Manager

import java.awt.*;
import java.awt.event.*;
class  Grid extends Frame
{
Grid(String s)
{
super(s);
setLayout(new GridLayout(3,4));
for(int i=1;i<=12;i++)
add(new Button("Button No"+i));
setVisible(true);
setSize(300,300);
addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            } });
} }
class Test3
{
public static void main(String[] args)
{
Grid f= new Grid("Grid Layout");
} }

Flow Layout Program - Layout Manager

import java.awt.*;
import java.awt.event.*;
class  Flow extends Frame
{
Flow(String s)
{
super(s);
setLayout(new FlowLayout());
for(int i=1;i<=9;i++)
add(new Button("Button No"+i));
setVisible(true);
setSize(300,300);
addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            } });
} }
class Test2
{
public static void main(String[] args)
{
Flow f= new Flow("Flow Layout");
} }