Sunday, May 26, 2013

Frame in Java

/**PROGRAM FOR FRAME (WINDOW)**/

import java.lang.*;
import java.awt.*;

class demo extends Frame
  {
   

     public static void main(String args[])throws Exception
          {
                 demo d=new demo();

    d.setSize(1600,800);

               d.setVisible(true);
          }
  }

                 

Editor in Java

/**EDITOR PROGRAM IN JAVA**/


import java.io.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;


public class editor extends Frame implements ActionListener
   {
      MenuBar mb;
      Menu m1,m2,m3,m4,m5;
      MenuItem a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,c1,c2,d1,e1,e2;
      TextArea t;

        editor()
          {
            t=new TextArea();
            mb=new MenuBar();
            m1=new Menu("FILE");
            a1=new MenuItem("NEW");
a2=new MenuItem("OPEN");
a3=new MenuItem("SAVE");
a4=new MenuItem("SAVE AS");
a5=new MenuItem("PRINT");
a6=new MenuItem("CLOSE");

         a1.addActionListener(this);
a2.addActionListener(this);
a3.addActionListener(this);
a4.addActionListener(this);
a5.addActionListener(this);
a6.addActionListener(this);

         

m2=new Menu("EDIT");
b1=new MenuItem("UNDO");
b2=new MenuItem("CUT");
b3=new MenuItem("COPY");
b4=new MenuItem("PASTE");
b5=new MenuItem("SELECT");
b6=new MenuItem("TIME");

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);

m3=new Menu("FORMAT");
c1=new MenuItem("WORDWRAP");
c2=new MenuItem("FONT");

c1.addActionListener(this);
c2.addActionListener(this);

m4=new Menu("VIEW");
d1=new MenuItem("STATUS BAR");

d1.addActionListener(this);

m5=new Menu("HELP");
e1=new MenuItem("HELP TOPICS");
e2=new MenuItem("ABOUT NOTEPAD");

e1.addActionListener(this);
e2.addActionListener(this);

             setLayout(new GridLayout(1,1));

           add(t);
           mb.add(m1); mb.add(m2);  mb.add(m3); mb.add(m4);  mb.add(m5);
           m1.add(a1); m1.add(a2); m1.add(a3); m1.add(a4); m1.add(a5);m1.add(a6);

          m2.add(b1); m2.add(b2); m2.add(b3); m2.add(b4); m2.add(b5);m2.add(b6);

          m3.add(c1); m3.add(c2);

          m4.add(d1);  m5.add(e1);  m5.add(e2);

       

setMenuBar(mb);  
       
        }

    public void actionPerformed(ActionEvent ae)
            {
  if(ae.getSource()==a6)
{
   System.exit(0);
}


         }




public static void main(String args[])
  {
    editor h= new editor();
    h.setTitle("Notepad");
    h.setVisible(true);
    h.setSize(500,500);
  }

         
    }

         


Mouse handling in Java

/**PROGRAM OF MOUSE HANDLING**/

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


class date1 extends Frame implements ActionListener,AdjustmentListener
  {

     Button a,b;
     Label l1,l2,l3,l4;
     TextField  t1,t2,t3,t4;
     Scrollbar s1,s2,s3;
     String g1,g2,g3;
      int da,mo,ye;
       int z,x,y;
               

     public date1()
{
  a=new Button("OK");
  b=new Button("EXIT");
  l1=new Label("DD");
  l2=new Label("MM");
  l3=new Label("YY");
  l4=new Label("DATE");

 t1=new TextField(5);
 t2=new TextField(10);
 t3=new TextField(5);
 t4=new TextField(20);

 s1=new Scrollbar(Scrollbar.HORIZONTAL,0,1,1,31);
 s2=new Scrollbar(Scrollbar.HORIZONTAL,0,1,1,12);
 s3=new Scrollbar(Scrollbar.HORIZONTAL,0,1,1900,2100);

            setLayout(new FlowLayout());

              add(l1);  add(s1);  add(t1);  add(l2);  add(s2);  add(t2);
  add(l3);  add(s3);  add(t3);
               add(a);  add(l4);  add(t4);  add(b);

 a.addActionListener(this);
 b.addActionListener(this);

 s1.addAdjustmentListener(this);
 s2.addAdjustmentListener(this);
 s3.addAdjustmentListener(this);
         
          }

      public void actionPerformed(ActionEvent ae)
           {
   
    if(ae.getSource()==a)
                  {
                   
          g1=t1.getText();
         
         
         g2=t2.getText();  

        g3=t3.getText();
       
                     t4.setText(g1+"/"+g2+"/"+g3);
     }

  if(ae.getSource()==b)
     {
        System.exit(0);
     }
}

         public void adjustmentValueChanged(AdjustmentEvent be)
{  
 
     da=s1.getValue();
      t1.setText(" "+da);

    mo=s2.getValue();
    t2.setText(" "+mo);

     ye=s3.getValue();
     t3.setText(" "+ye);

}

public static void main(String args[])throws Exception
 {
                 date1 q=new date1();

    q.setSize(400,400);
    q.setVisible(true);
}

      }
         



Date In Java

/***PROGRAM FOR DATE*/

import java.io.*;

class date
{
   int dd,mm,yy;

public void getdate()throws Exception
{
  int flag=0;
   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 
  while(flag==0)
   {
                   
       try
{
   System.out.println("ENTER THE DATE:");
     dd=Integer.parseInt(br.readLine());
     mm=Integer.parseInt(br.readLine());
     yy=Integer.parseInt(br.readLine());

flag=1;
}

catch(NumberFormatException ne)
  {
    System.out.println("Exception occured: INVALID NUMBER" +ne);
  }
                  };
               }

    public void putdate()
{
    System.out.println(dd+"/"+mm+"/"+yy);
}
 
           public static void main(String args[])throws Exception
       {
                      date d=new date();

                          d.getdate();
  d.putdate();
                  }
  }

Calculator in Java

/**JAVA PROGRAM FOR CALCULATOR**/


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

class calc extends Frame implements ActionListener
   {
        int val1,val2,res,flag;
            TextField t1;
          Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17;

           public calc()
            {
               b0=new Button("0");
               b1=new Button("1");
   b2=new Button("2");
  b3=new Button("3");
 b4=new Button("4");
  b5=new Button("5");
  b6=new Button("6");
  b7=new Button("7");
  b8=new Button("8");
  b9=new Button("9");
  b10=new Button("+");
  b11=new Button("-");
  b12=new Button("*");
  b13=new Button("/");
  b14=new Button(".");
  b15=new Button("=");
  b16=new Button("OFF");
  b17=new Button("CLEAR");

  t1=new TextField(5);

setLayout(new FlowLayout());
       
           add(t1); add(b17); add(b16); add(b1); add(b2);  add(b3);  add(b10); add(b4); add(b5); add(b6);
add(b11);  add(b7);  add(b8); add(b9);  add(b12);  add(b0); add(b14);  add(b15); add(b13);

  b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);

        }

       public void actionPerformed(ActionEvent ae)
{
 
   if(ae.getSource()==b0)
       {
       t1.setText(t1.getText()+"0");
     }

  if(ae.getSource()==b1)
       {
       t1.setText(t1.getText()+"1");
     }

if(ae.getSource()==b2)
       {
       t1.setText(t1.getText()+"2");
     }

if(ae.getSource()==b3)
       {
       t1.setText(t1.getText()+"3");
     }

if(ae.getSource()==b4)
       {
       t1.setText(t1.getText()+"4");
     }

if(ae.getSource()==b5)
       {
       t1.setText(t1.getText()+"5");
     }

if(ae.getSource()==b6)
       {
       t1.setText(t1.getText()+"6");
     }

if(ae.getSource()==b7)
       {
       t1.setText(t1.getText()+"7");
     }

if(ae.getSource()==b8)
       {
       t1.setText(t1.getText()+"8");
     }

if(ae.getSource()==b9)
       {
       t1.setText(t1.getText()+"9");
     }

if(ae.getSource()==b10)
       {
         val1=Integer.parseInt(t1.getText());
                      t1.setText("");
          flag=1;
     }



if(ae.getSource()==b11)
       {
         val1=Integer.parseInt(t1.getText());
                      t1.setText("");
          flag=2;
     }



if(ae.getSource()==b12)
       {
         val1=Integer.parseInt(t1.getText());
                      t1.setText("");
          flag=3;
     }



if(ae.getSource()==b13)
       {
         val1=Integer.parseInt(t1.getText());
                      t1.setText("");
          flag=4;
     }



if(ae.getSource()==b17)
       {
                      t1.setText("");    
     }



if(ae.getSource()==b16)
       {
       
        System.exit(0);      
    }

         

if(ae.getSource()==b14)
       {
         val1=Integer.parseInt(t1.getText());
                      t1.setText(val1+".");

                     
     }



if(ae.getSource()==b15)
       {
                      val2=Integer.parseInt(t1.getText());
   

         switch(flag)
              {
   
      case 1: res=val1+val2;
break;
      case 2: res=val1-val2;
break;

      case 3: res=val1*val2;
break;

      case 4: res=val1/val2;
break;
}

  t1.setText(""+res);
                }

         }

                 public static void main(String args[])
{

    calc y=new calc();

     y.setSize(100,200);
     y.setVisible(true);
                       }
           }

Action Listener

/**ACTION LISTENER IN JAVA EXAMPLE**/

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

class bb extends Frame implements ActionListener
 {
 
    Button b;
    TextField t;

      public bb()
{
   b=new Button("CLICK:");
                t=new TextField(20);

          setLayout(new FlowLayout());

             add(b);    add(t);

              b.addActionListener(this);
         }

         public void actionPerformed(ActionEvent ae)
               {
                   if(ae.getSource()==b)
                      {
                          Frame f=new Frame();
                            {
                                f.setSize(50,50);
                                f.setVisible(true);
                           }
                      }
               }

          public static void main(String args[])
            {
               
                 bb a=new bb();
         
                    a.setSize(400,600);
                    a.setVisible(true);

             }
     }


             

Addition Of Numbers

/*Example of class for addition of the two numbers*/

import java.awt.*;
import java.awt.event.*;
class as extends Frame implements ActionListener
  {
        TextField t1,t2,t3;    
         Button b1,b2,b3;

            public as()
              {
                     t1=new TextField(10);
         t2=new TextField(10);
                     t3=new TextField(10);
                      b1=new Button("ADD");
          b2=new Button("SUB");
          b3=new Button("EXIT");

              setLayout(new FlowLayout());
             
              add(b1); add(t1); add(b2);       add(t2);  add(t3);   add(b3);
             
                b1.addActionListener(this);
    b2.addActionListener(this);
                b3.addActionListener(this);
 
          }

            public static void main(String args[])throws Exception
   {
   
      as a=new as();

                  a.setSize(200,300);
                   a.setVisible(true);
             }

        public void actionPerformed(ActionEvent ae)
  {
 
                 int val1=Integer.parseInt(t1.getText());
     int val2=Integer.parseInt(t2.getText());
     int res;

if(ae.getSource()==b1)
   {
                    res=val1+val2;
                     t3.setText(" "+res);
               }

           
if(ae.getSource()==b2)
   {
                    res=val1-val2;
                     t3.setText(" "+res);
               }

           
if(ae.getSource()==b3)
   {
                    System.exit(0);
               }
      }
  }
    

Simple Applet Program

Following is a Simple applet program inn java.



import java.awt.*;
import java.applet.*;

public class app2 extends Applet
  {
     public void paint(Graphics g)
       {
             g.drawString("MY FIRST APPLET",30,30);
       }
  }

Applet in Java

HTML FILE CODE:

Create a file name sample.html and write the following code in it.

<applet code="app1"width=600 height=600></applet>


Then create a java file and write the following code in it.


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

public class app1 extends Applet implements ActionListener
 {
   
    Button b;
    TextField t;

      public void Init()
{
   b=new Button("CLICK:");
                t=new TextField(20);

          setLayout(new FlowLayout());

             add(b);    add(t);

              b.addActionListener(this);
         }

         public void actionPerformed(ActionEvent ae)
               {
                   if(ae.getSource()==b)
                      {
                          t.setText("YOUR NAME HERE:");
                      }
               }

         
     }

 
              

Simple Event Program In Java

/*EVENT PROGRAM IN JAVA*/

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

class aaa extends Frame implements ActionListener
 {

     Button b1,b2,b3,b4,b5,b6;
     Label a,b,c,d,r,t;
     TextField a1,a2,a3,a4,a5,a6,a7;
       int a1;

           public aaa()
{
   b1=new Button("SUBMIT:");
   b2=new Button("RESET:");
   b3=new Button("EXIT:");
   b4=new Button("TOTAL:");
   b5=new Button("NEW:");
   b6=new Button("OLD:");

   a=new Label("NAME:");
   b=new Label("AGE:");
   c=new Label("GENDER:");
   d=new Label("EMAIL_ID:");
   r=new Label("MOBILE NUMBER:");
   t=new Label("NATIONALITY:");

   a1=new TextField(20);
   a2=new TextField(20);
   a3=new TextField(20);
   a4=new TextField(20);
   a5=new TextField(20);
   a6=new TextField(20);
   a7=new TextField(20);

    setLayout(new FlowLayout());
                  add(b5);  add(b6);
   
                   

                b1.addActionListener(this);
                b2.addActionListener(this);
   b3.addActionListener(this);
   b4.addActionListener(this);
   b5.addActionListener(this);
   b6.addActionListener(this);

          }

public aaa(int a1)
{
                 if(a1==0)
          {
            setLayout(new FlowLayout());
                           add(a);  add(b);  add(c);  add(d);  add(r);  add(t);
               add(b1);  add(b2);  add(b3);
          }
 }

          public void actionPerformed(ActionEvent ae)
{
  if(ae.getSource()==b5)
     {
        aaa(0);
     }

   if(ae.getSource()==b6)
      {
       
      setLayout(new FlowLayout());
            add(a);  add(b);  add(c);  add(d);  add(r);  add(t);
               add(b1);  add(b2);  add(b3);  add(b4);
       }
             
                  if(ae.getSource()==b3)
       {
                         System.exit(0);
       }
             

            }

public static void main(String args[])throws Exception
 {

aaa g=new aaa();

                       g.setSize(300,600);

          g.setVisible(true);

             }

      }

       
 

MS SQL : How to identify fragmentation in your indexes?

Almost all of us know what fragmentation in SQL indexes are and how it can affect the performance. For those who are new Index fragmentation...