Tuesday, April 19, 2016

Create Thread by Extending Thread Class:

class ThreadDemo extends Thread {
   private Thread t;
   private String threadName;

   ThreadDemo( String name){
       threadName = name;
       System.out.println("Creating " +  threadName );
   }
   public void run() {
      System.out.println("Running " +  threadName );
      try {
         for(int i = 4; i > 0; i--) {
            System.out.println("Thread: " + threadName + ", " + i);
            // Let the thread sleep for a while.
            Thread.sleep(50);
         }
     } catch (InterruptedException e) {
         System.out.println("Thread " +  threadName + " interrupted.");
     }
     System.out.println("Thread " +  threadName + " exiting.");
   }

   public void start ()
   {
      System.out.println("Starting " +  threadName );
      if (t == null)
      {
         t = new Thread (this, threadName);
         t.start ();
      }
   }

}

public class TestThread2 {
   public static void main(String args[]) {

      ThreadDemo T1 = new ThreadDemo( "Thread-1");
      T1.start();

      ThreadDemo T2 = new ThreadDemo( "Thread-2");
      T2.start();
   }
}


Create Thread by Implementing Runnable Interface:


   class ThreadDemo extends Thread {
   private Thread t;
   private String threadName;

   ThreadDemo( String name){
       threadName = name;
       System.out.println("Creating " +  threadName );
   }
   public void run() {
      System.out.println("Running " +  threadName );
      try {
         for(int i = 4; i > 0; i--) {
            System.out.println("Thread: " + threadName + ", " + i);
            // Let the thread sleep for a while.
            Thread.sleep(50);
         }
     } catch (InterruptedException e) {
         System.out.println("Thread " +  threadName + " interrupted.");
     }
     System.out.println("Thread " +  threadName + " exiting.");
   }

   public void start ()
   {
      System.out.println("Starting " +  threadName );
      if (t == null)
      {
         t = new Thread (this, threadName);
         t.start ();
      }
   }

}

public class TestThread1 {
   public static void main(String args[]) {

      ThreadDemo T1 = new ThreadDemo( "Thread-1");
      T1.start();

      ThreadDemo T2 = new ThreadDemo( "Thread-2");
      T2.start();
   }
}

Wednesday, February 17, 2016

Program to add Scrolling List in a Frame.

import java.awt.*;
class ListAdd extends Frame
{
public static void main(String args[ ] )
{
List lt= new List( 5 , true);
lt.add ( "Window 98" );
lt.add ( "Window 2000" );
lt.add ( "Window XP" );
lt.add ( "Window NT" );
ListAdd f1=new ListAdd();
f1.setTitle("Adding a List in a Frame!!!");
f1.setLayout(new FlowLayout());
f1.add(lt);
f1.setSize(500,300);
f1.setVisible(true);
}
}
Output:

Tuesday, February 16, 2016

Program to add choice menu in Frame.

import java.awt.*;
class Choice_Test extends Frame
{
public static void main(String arg[])
{
Choice ch = new Choice ( );
ch.add ( "Apples" );
ch.add ( "Oranges" );
ch.add ( "Strawberries" );
ch.add ( "Bananas");
Choice_Test f1=new Choice_Test();
f1.setTitle("Adding a Choice menu in a Frame!!!");
f1.setLayout(new FlowLayout());
f1.add(ch);
f1.setSize(500,300);
f1.setVisible(true);
}
}
Output:


Monday, February 15, 2016

Program to add RadioButton in Frame.

import java.awt.*;

class RadioButton extends Frame
{
public static void main(String args[ ] )
{
CheckboxGroup og=new CheckboxGroup();
Checkbox male=new Checkbox("Male",og,true);
Checkbox female=new Checkbox("Female",og,false);
RadioButton f1 =new RadioButton();
f1.setTitle("Adding a RadioButton in a Frame!!!");
f1.setLayout(new FlowLayout());
f1.add(male);
f1.add(female);
f1.setSize(500,300);
f1.setVisible(true);

}
}
Output:

Saturday, February 13, 2016

Program to add checkbox in Frame.

import java.awt.*;
class chkBox extends Frame
{
public static void main(String args[ ] )
{
Checkbox c1 =new Checkbox("Reading");
Checkbox c2=new Checkbox("Singing");
Checkbox c3=new Checkbox("Dancing");
c1.setState(true);
chkBox f1=new chkBox();
f1.setTitle("Adding a Checkbox in a Frame!!!");
f1.setLayout(new FlowLayout());
f1.add(c1);
f1.add(c2);
f1.add(c3);
f1.setSize(500,300);
f1.setVisible(true);
}
}
Output:

Friday, February 12, 2016

Program to add buttons in Frame.

import java.awt.*;
class btnAdd extends Frame
{
public static void main(String args[ ] )
{
Button b1=new Button("Add");
Button b2=new Button("Subtract");
Button b3=new Button("Mu|tip|y");
Button b4=new Button("Division");
btnAdd f1=new btnAdd();
f1 .setTitle("Adding Buttons in a Frame!!!");
f1 .setLayout(new FlowLayout());
f1 .add(b1 );
f1 .add(b2);
f1 .add(b3);
f1 .add(b4);
f1 .setSize(500,500);
f1 .setVisible(true );
}
}
Output: