Wednesday, 17 July 2013

program for a form window in java

import java.awt.*;
import java.awt.event.*;

class MyFrame extends Frame implements ActionListener
{
 Checkbox cMl,cFl,cCs,cIt,cEc;
 Checkbox cJava,cPython,cRuby,cNet;
 Button bOk,bCancle,bExit;

 CheckboxGroup cbg1,cbg2;

 MyFrame()
 {
  super("FORM");
  setSize(400,350);
  setBackground(new Color(200,200,255));

  Label lSex=new Label("Sex");
  lSex.setFont(new Font("lucida console",Font.PLAIN,18));

  Label lBranch=new Label("Branch");
  lBranch.setFont(new Font("lucida console",Font.PLAIN,18));

  Label lTech=new Label("Technologies");
  lTech.setFont(new Font("lucida console",Font.PLAIN,18));

  cJava=new Checkbox("Java");
  cJava.setFont(new Font("lucida console",Font.PLAIN,18));
  cPython=new Checkbox("Python");
  cPython.setFont(new Font("lucida console",Font.PLAIN,18));
  cRuby=new Checkbox("Ruby");
  cRuby.setFont(new Font("lucida console",Font.PLAIN,18));
  cNet=new Checkbox(".Net");
  cNet.setFont(new Font("lucida console",Font.PLAIN,18));
 
  cbg1=new CheckboxGroup();
  cMl=new Checkbox("Male",cbg1,true);
  cMl.setFont(new Font("lucida console",Font.PLAIN,18));
  cFl=new Checkbox("Female",cbg1,false);
  cFl.setFont(new Font("lucida console",Font.PLAIN,18));

  cbg2=new CheckboxGroup();
  cCs=new Checkbox("CS",cbg2,true);
  cCs.setFont(new Font("lucida console",Font.PLAIN,18));
  cIt=new Checkbox("IT",cbg2,false);
  cIt.setFont(new Font("lucida console",Font.PLAIN,18));
  cEc=new Checkbox("EC",cbg2,false);
  cEc.setFont(new Font("lucida console",Font.PLAIN,18));

  bOk=new Button("OK");
  bOk.setFont(new Font("lucida console",Font.PLAIN,18));
  bOk.setBackground(new Color(200,255,200));
 
  bCancle=new Button("Cancle");
  bCancle.setFont(new Font("lucida console",Font.PLAIN,18));
  bCancle.setBackground(new Color(255,200,200));

  bExit=new Button("Exit");
  bExit.setFont(new Font("lucida console",Font.PLAIN,18));
  bExit.setBackground(new Color(220,220,255));

  Panel p1=new Panel();
  p1.setLayout(new BorderLayout(57,10));

   Panel p11=new Panel();
   p11.setLayout(new GridLayout(1,2,10,10));
   p11.add(cMl);
   p11.add(cFl);

  p1.add(lSex,BorderLayout.WEST);
  p1.add(p11,BorderLayout.CENTER);


  Panel p2=new Panel();
  p2.setLayout(new BorderLayout(30,10));

   Panel p21=new Panel();
   p21.setLayout(new GridLayout(1,3,10,10));
   p21.add(cCs);
   p21.add(cIt);
   p21.add(cEc);

  p2.add(lBranch,BorderLayout.WEST);
  p2.add(p21,BorderLayout.CENTER);

  Panel p3=new Panel();
  p3.setLayout(new BorderLayout(10,10));
  
   Panel p31=new Panel();
   p31.setLayout(new GridLayout(1,4,10,10));
   p31.add(cJava);
   p31.add(cPython);
   p31.add(cRuby);
   p31.add(cNet);
 
  p3.add(lTech,BorderLayout.NORTH);
  p3.add(p31,BorderLayout.CENTER);

  Panel p4=new Panel();
  p4.setLayout(new BorderLayout(10,10));

   Panel p41=new Panel();
   p41.setLayout(new GridLayout(1,3,20,20));
   p41.add(bOk);
   p41.add(bCancle);
   p41.add(bExit);

  p4.add(new Label(),BorderLayout.NORTH);
  p4.add(p41,BorderLayout.CENTER);
  
  setLayout(new GridLayout(4,1,10,10));
  add(p1);
  add(p2);
  add(p3);
  add(p4);

  addWindowListener(new MyWndAdapter(this));

  bOk.addActionListener(this);
  bCancle.addActionListener(this);
  bExit.addActionListener(this);
 
 }

 public Insets getInsets()
 {
  return new Insets(50,20,20,20);
 }

 public void actionPerformed(ActionEvent ae)
 {
  if(ae.getSource()==bOk)
  {
   String str="Sex          : ";
   str=str+cbg1.getSelectedCheckbox().getLabel();
  
   str=str+"\nBranch       : "+cbg2.getSelectedCheckbox().getLabel();

   str=str+"\nTechnologies : ";
   if(cJava.getState())
    str=str+"Java  ";
   if(cPython.getState())
    str=str+"Python  ";
   if(cRuby.getState())
    str=str+"Ruby  ";
   if(cNet.getState())
    str=str+".Net  ";

   System.out.println(str);

  }
  else
  if(ae.getSource()==bCancle)
  {
  
  }
  else
  if(ae.getSource()==bExit)
  {
   dispose();
   System.exit(0);
  }
 }

}


class MyWndAdapter extends WindowAdapter
{
 MyFrame mf;

 MyWndAdapter(MyFrame mf)
 {
  this.mf=mf;
 }

 public void windowClosing(WindowEvent we)
 {
  mf.dispose();
  System.exit(0);
 }
}

class frm
{
 public static void main(String arg[])
 {
  MyFrame mf=new MyFrame();
  mf.setVisible(true);
 }

-------------------------------------------------------------------------------------------------------------
steps to run the Demo program of Form

step1: save the file as frm.java file in the bin and refresh it.

step2: after that open ur command prompt and write the path of ur jdk
       
       like my jdk path is
       C:\java\jdk1.7.0_10\bin
step3: if u not know how to make path then follow the steps below






step4: after that to execute the program do the following

C:\java\jdk1.7.0_10\bin>javac frm.java and press enter



step5: if the step4 is successfull then perform this step

C:\java\jdk1.7.0_10\bin>java frm

then a window appears like this




and enjoy!!  ;)

if u have any problem then msg me at
rawlot007bhati@gmail.com

No comments:

Post a Comment