NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

Sunday, October 21, 2012

Java program to illustrate swing concept


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class swing extends JFrame implements ActionListener,ItemListener
{JComboBox combo=new JComboBox();
  JButton btnExit=new JButton("Exit");
  Container cp=getContentPane();
  Color c=cp.getBackground();
  public swing()
  {        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLayout(null);
            combo.addItem("Reset to Default Color");
            combo.addItem("Set background color to red");
            combo.addItem("Set background color to green");
            combo.addItem("Set background color to blue");
            cp.add(combo);
            cp.add(btnExit);
            btnExit.setBounds(85,120,70,30);
            combo.setBounds(20,10,200,30);
            combo.addItemListener(this);
            btnExit.addActionListener(this);
  }
public void itemStateChanged(ItemEvent e)
  {        String s=(String)e.getItem();
            if(s.compareTo("Reset to Default Color")==0)
            cp.setBackground(c);
            if(s.compareTo("Set background color to red")==0)
            cp.setBackground(Color.red);
            if(s.compareTo("Set background color to green")==0)
            cp.setBackground(Color.green);
            if(s.compareTo("Set background color to blue")==0)
            cp.setBackground(Color.blue);
  }
public void actionPerformed(ActionEvent a)
  {        String s=a.getActionCommand();
            if(s.equals("Exit"))
            System.exit(0);
  }
public static void main(String[]args)
{
            swing cd=new swing();
            cd.setSize(new Dimension(250,200));
            cd.setTitle("A swing application");
            cd.setVisible(true);
   }
 }
Output

Java program to create student registration form


import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class student extends Frame implements ActionListener
{String msg;
  Button b1=new Button("save");
  Label l11=new Label("Student details",Label.CENTER);
  Label l1=new Label("Name:",Label.LEFT);
  Label l2=new Label("age:",Label.LEFT);
  Label l3=new Label("Sex(M/F):",Label.LEFT);
  Label l4=new Label("Address:",Label.LEFT);
  Label l5=new Label("Course:",Label.LEFT);
  Label l6=new Label("Semester:",Label.LEFT);
  Label l7=new Label("",Label.RIGHT);
  TextField t1=new TextField();
  Choice c1=new Choice();
  CheckboxGroup cbg=new CheckboxGroup();
  Checkbox ck1=new Checkbox("Male",false,cbg);
  Checkbox ck2=new Checkbox("Female",false,cbg);
  TextArea t2=new TextArea("",180,90,TextArea.SCROLLBARS_VERTICAL_ONLY);
  Choice course=new Choice();
  Choice sem=new Choice();
  Choice age=new Choice();
public student()
 {addWindowListener(new myWindowAdapter());
  setBackground(Color.cyan);
  setForeground(Color.black);
  setLayout(null);
  add(l11);
  add(l1);
  add(l2);
  add(l3);
  add(l4);
  add(l5);
  add(l6);
  add(l7);
  add(t1);
  add(t2);
  add(ck1);
  add(ck2);
  add(course);
  add(sem);
  add(age);
  add(b1);
  b1.addActionListener(this);
  add(b1);
  course.add("BSc c.s");
  course.add("BSc maths");
  course.add("BSc physics");
  course.add("BA English");
  course.add("BCOM");
  sem.add("1");
  sem.add("2");
  sem.add("3");
  sem.add("4");
  sem.add("5");
  sem.add("6");
  age.add("17");
  age.add("18");
  age.add("19");
  age.add("20");
  age.add("21");
  l1.setBounds(25,65,90,20);
  l2.setBounds(25,90,90,20);
  l3.setBounds(25,120,90,20);
  l4.setBounds(25,185,90,20);
  l5.setBounds(25,260,90,20);
  l6.setBounds(25,290,90,20);
  l7.setBounds(25,260,90,20);
  l11.setBounds(10,40,280,20);
  t1.setBounds(120,65,170,20);
  t2.setBounds(120,185,170,60);
  ck1.setBounds(120,120,50,20);
  ck2.setBounds(170,120,60,20);
  course.setBounds(120,260,100,20);
  sem.setBounds(120,290,50,20);
  age.setBounds(120,90,50,20);
  b1.setBounds(120,350,50,30);
}
public void paint(Graphics g)
{g.drawString(msg,200,450);}
public void actionPerformed(ActionEvent ae)
{if(ae.getActionCommand().equals("save"))
  {msg="Student details saved!";
   setForeground(Color.red); }
}
public static void main(String g[])
{student stu=new student();
 stu.setSize(new Dimension(500,500));
 stu.setTitle("student registration");
 stu.setVisible(true);
}
}
 class myWindowAdapter extends WindowAdapter
{public void windowClosing(WindowEvent we)
 {
  System.exit(0);
 }
}
Output

Java program to implement single inheritance


import java.io.*;
  class employee
  {
    private String empname;
    private  int empcode;

  void read(String s,int p)
  {
    empname= s;
    empcode=p;
  }
 void display()
  {
    System.out.println("The employee name is :" +empname);
    System.out.println("The employee code is :"+empcode);
  }
 }

 class labourer extends employee
 
  { private String labourtype;
    private int laboursalary;

  void read1()throws IOException
 
    {
       BufferedReader br= new BufferedReader( new InputStreamReader (System.in));
       System.out.println("Enter the employee name:");
       String a=br.readLine();
       System.out.println("Enter the employee code :");
       int p= Integer.parseInt(br.readLine());
       read(a,p);
       System.out.println("Enter the labourtype :");
       labourtype = br.readLine();
       System.out.println("Enter the labour salary :");
       laboursalary=Integer.parseInt(br.readLine());
    }

   void display1()
 
    {
    
      display();
      System.out.println(" The labour type is : "+ labourtype);
      System.out.println(" The labour salary is :"+laboursalary);   }
 }
class single   
    { public static void main (String s[])throws IOException
      {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the number of employees");
        int n = Integer.parseInt(br.readLine());
        labourer l[]= new labourer[n];
        for(int i=0; i<n; i++)
        l[i]= new labourer();
        for(int i=0; i<n; i++)
        l[i] .read1();
        for(int i=0; i<n; i++)
      l[i].display1(); }
    }
Output

Java program to animated text or moving text


import java.applet.*;
import java.awt.*;
/*<applet code="runtext" width=450 height=400>
</applet>*/

public class runtext extends Applet implements Runnable
 {
   Thread t;
   int x,y;
  boolean stopflag;
            public void init()
             {
               setBackground(Color.black);
               setForeground(Color.white);
               x=120;y=130;
              
             }

            public void start()
              {
                stopflag=false;
                t=new Thread(this,"one");
                t.start();
                       
              }

            public void paint(Graphics g)
              {showStatus("name of college");
                g.drawString("SS College",x,y);
              }

            public void stop()
              {
                stopflag=true;
                t=null;
              }

            public void run()
              {
                for(; ;)
                        {
                          try
                            {
                                    y++;x++;
                                    repaint();
                                    Thread.sleep(50);
                                    if(stopflag) break;
                            }
                         catch(InterruptedException e)
                          {
                                    System.out.println(e);
                          }
                        }
             }
}
Output

Java program to illustrate menubar with the help of applet


import java.applet.*;
import java.awt.*;
 class mymenu extends Frame
{
            MenuBar mb;
            Menu filemenu,editmenu,viewmenu;
            MenuItem open,newmi,save,cut,copy;
            CheckboxMenuItem toolbar,layer;

public mymenu()
{
            mb=new MenuBar();
            setMenuBar(mb);
           
            filemenu=new Menu("File");
            editmenu=new Menu("Edit");
            viewmenu=new Menu("View");

            open=new MenuItem("Open");
            newmi=new MenuItem("New");
            save=new MenuItem("Save");
            cut=new MenuItem("Cut");
            copy=new MenuItem("Copy");

            toolbar=new CheckboxMenuItem("Toolbar",true);
            layer=new CheckboxMenuItem("Layer Window",false);

            filemenu.add(open);
            filemenu.add(newmi);
            filemenu.add(save);

            editmenu.add(cut);
            editmenu.add(copy);

            viewmenu.add(toolbar);
            viewmenu.add(layer);

            mb.add(filemenu);
            mb.add(editmenu);
            mb.add(viewmenu);
}

public static void main(String s[])
{
            mymenu mn=new mymenu();
             mn.setSize(new Dimension(500,500));
            mn.setTitle("Menu Bar");
            mn.setVisible(true);
}
}
Output

Java program to create login form using applet


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Loginform extends Frame implements ActionListener
{ Label l1=new Label("user name");
  Label l2=new Label("password");
  Label l3=new Label(" ");
  TextField t1=new TextField();
  TextField t2=new TextField();
  Button b= new Button("Submit");
  public Loginform()
  { add(l1);
    add(t1);
    add(l2);
    add(t2);
    add(b);
    add(l3);
    l1.setBounds(20,45,70,20);
    t1.setBounds(180,45,200,20);
    l2.setBounds(20,95,70,20);
    t2.setBounds(180,95,200,20);
    b.setBounds(310,145,70,20);
    b.addActionListener(this);
    t2.setEchoChar('*');
    addWindowListener(new mwa());
  }
  public void actionPerformed(ActionEvent e)
  { l3.setText("Welcome "+t1.getText());}
  public static void main(String s[])
  { Loginform l=new Loginform();
    l.setSize(new Dimension(600,600));
    l.setTitle("Login");
    l.setVisible(true);
  }
}
class mwa extends WindowAdapter
{ public mwa(){}
  public void windowClosing(WindowEvent e)
  { System.exit(0);
  }
}
Output