You are on page 1of 69

--------------------------------------------------------------------Class No. Topics to be covered No.

of programs in the folder -------------------------------------------------------(1) Introduction ---(2) Class and object 12 (3) Inheritance 04 (4) String 07 (5) Array 03 (6) Array object 01 (7) Exception handling 02 (8) File handling 05 (9) Thread 08 (10) Applet 16 (11) Swing ---(12) Event handling 05 (13) Awt 06 (14) Interface ---(15) Package 02 --------------------------------------------------------------------Total 15 classes and total number of programs will be .... 71 ---------------------------------------------------------------------

Topic : Class and object


Program Name : CLASS1~1 JAV (Class1.java) import javax.swing.*; class class1 { String name; int rno; String rrno; public void getdata() { name=JOptionPane.showInputDialog("Enter name"); rrno=JOptionPane.showInputDialog("Enter rollno"); rno=Integer.parseInt(rrno); }

12

public void printdata() { JOptionPane.showMessageDialog(null,"The name "+name+" the roll nuber "+rno); } public static void main(String args[]) { String ans=new String(); class1 b1=new class1(); while(!ans.equals("no")) { b1.getdata(); b1.printdata(); ans=JOptionPane.showInputDialog("Enter yes/no"); } JOptionPane.showMessageDialog(null,"GOOD BYe"); System.exit(0); } }

Program Name : DRIVER~1 JAV (driver.java) public class driver { private int dno; char t; public driver(int a,char s) { dno=a; t=s; } public void printdata() { System.out.println("Door No : "+dno); if(t=='l') System.out.println("Driver Type : "+t); else System.out.println("Driver Type : "+t); } public static void main(String agrs[]) { driver obj1=new driver(5,'r'); obj1.printdata(); }

Program Name : FIRST~1 JAV (first.java) public class first { int s,y,sum; void init(int a,int b) { s=a; y=b; } void calculation() { sum=s+y; } void printdata() { System.out.println("The sum of the two number is "+sum); } public static void main(String args[]) { first abc1=new first(); abc1.init(20,30); abc1.calculation(); abc1.printdata(); } } Program Name : INPUTM~1 JAV (inputmessage.java) import javax.swing.*;

3
public class inputmessage { public static void main(String args[]) { String first,second; int number1,number2,number3; first=JOptionPane.showInputDialog("Enter the first number"); second=JOptionPane.showInputDialog("Enter the second number"); number1=Integer.parseInt(first); number2=Integer.parseInt(second); number3=number1+number2; JOptionPane.showMessageDialog(null,"Sum of the two number is "+number3); System.out.println ("Sum of the two number is "+number3); System.exit(0); } } Program Name : INPUTM~2 JAV (inputmessage1.java) import javax.swing.*; public class inputmessage1 { public static void main(String args[]) { String f,s,t; int n1,n2,n3; f=JOptionPane.showInputDialog("Enter the first number"); s=JOptionPane.showInputDialog("Enter the second number"); t=JOptionPane.showInputDialog("Enter the third number"); n1=Integer.parseInt(f); n2=Integer.parseInt(s); n3=Integer.parseInt(t); if(n1==n2 && n1==n3 && n2==n3) { JOptionPane.showMessageDialog(null,"Numbers are equal"+n1); } else if(n1>=n2 && n1>=n3) { if(n2>n3) JOptionPane.showMessageDialog(null,"Second greatest"+n2); else JOptionPane.showMessageDialog(null,"Second greatest"+n3); } else if(n2>=n1 && n2>=n3) { if(n1>n3) JOptionPane.showMessageDialog(null,"Second greatest"+n1); else JOptionPane.showMessageDialog(null,"Second greatest"+n3); } else {

if(n1>n2) JOptionPane.showMessageDialog(null,"Second greatest"+n1); else JOptionPane.showMessageDialog(null,"Second greatest"+n2); } } } Program Name : MESSAG~1 JAV (message.java) import javax.swing.*; public class message { public static void main(String args[]) { JOptionPane.showMessageDialog(null,"Welcome to java"); System.exit(0); } } Program Name : TEST~1 import java.io.*; JAV (test.java) System.exit(0);

public class test { private int sub1,sub2,sub3,sub4,sub5,total; public test(int a,int b,int c,int d,int e) { sub1=a; sub2=b; sub3=c; sub4=d; sub5=e; total=0; } public void printdata() { System.out.println("Total marks = "+total); } public void calculation() { total=sub1+sub2+sub3+sub4+sub5; } public static void main(String args[]) { System.out.println("Welcome To java Programming"); test obj1=new test(50,50,50,50,50); obj1.calculation(); obj1.printdata(); } JAV (test1.java)

Program Name : TEST1~2 import java.io.*;

public class test1 { private int sub1,sub2,sub3,sub4,sub5; float total; float avg; public test1(int a,int b,int c,int d,int e) { sub1=a; sub2=b; sub3=c; sub4=d; sub5=e; total=0; } float printdata() { return avg; } void calculation() { total=sub1+sub2+sub3+sub4+sub5; avg=total/5; } public static void main(String args[]) { float y; System.out.println("Welcome To java Programming"); test1 obj1=new test1(50,39,50,29,50); obj1.calculation(); y=obj1.printdata(); System.out.println("Average = "+y); } } Program Name : SALARY~1 JAV (salary2.java) import javax.swing.*; public class salary1 { int s1,hra,da,it,gsal; String output; void init(int salary) { s1=salary; } void calculation() { hra=(s1 *10)/100; da=(s1*20)/100; it=(s1*1)/100; gsal=(s1+hra+da)-it; } void printdata()

{ output="Basic Salary of an Employee = "+s1; output+="\nHra = "+hra; output+="\nDa = "+da; output+="\nIT = "+it; output+="\nGross Salary of an Employee = "+gsal; JOptionPane.showMessageDialog(null,output); } public static void main(String args[]) { String sal; String ans=new String(); int sal1; while(!ans.equals("no")) { sal=JOptionPane.showInputDialog(null,"Enter the salary"); sal1=Integer.parseInt(sal); salary1 employee=new salary1(); employee.init(sal1); employee.calculation(); employee.printdata(); ans=JOptionPane.showInputDialog(null,"yes/no"); } JOptionPane.showMessageDialog(null,"Good bye...."); System.exit(0); } } Program Name : SALARY~2 JAV (salary.java) import javax.swing.*; public class salary2 { int s1,hra,da,it,gsal; String output; salary2(int salary) { s1=salary; } salary2() { s1=1000; } salary2(int salary,int a,int b, int c) { s1=salary; hra=a; da=b; it=c; } void calculation() {

hra=(s1 *10)/100; da=(s1*20)/100; it=(s1*1)/100; gsal=(s1+hra+da)-it; } void printdata() { output="Basic Salary of an Employee = "+s1; output+="\nHra = "+hra; output+="\nDa = "+da; output+="\nIT = "+it; output+="\nGross Salary of an Employee = "+gsal; JOptionPane.showMessageDialog(null,output); } public static void main(String args[]) { String sal,h,d,i; String ans=new String(); int sal1,h1,d1,i1; sal=JOptionPane.showInputDialog(null,"Enter the salary"); h=JOptionPane.showInputDialog(null,"Enter the hra"); d=JOptionPane.showInputDialog(null,"Enter the da"); i=JOptionPane.showInputDialog(null,"Enter the it"); sal1=Integer.parseInt(sal); h1=Integer.parseInt(h); d1=Integer.parseInt(d); i1=Integer.parseInt(i); salary2 salary2 salary2 employee=new salary2(sal1); emp=new salary2(); emp1=new salary2(sal1,h1,d1,i1);

employee.calculation(); employee.printdata(); emp.calculation(); emp.printdata(); emp1.calculation(); emp1.printdata(); JOptionPane.showMessageDialog(null,"Good bye...."); System.exit(0); }

Program Name : SALARY~3 JAV (salary1.java) import javax.swing.*; public class salary3 { int s1; double hra,da,it,gsal; String output; salary3(int salary) { s1=salary; }

8
void calculation() { if (s1>=1000) { hra=(s1*10)/100; da=(s1*20)/100; it=0; } if(s1>1000 && s1<=3000) { hra=(s1*15)/100; da=(s1*25)/100; it=(s1*1.5)/100; } gsal=(s1+hra+da)-it; } void printdata() { output="Basic Salary of an Employee = "+s1; output+="\nHra = "+hra; output+="\nDa = "+da; output+="\nIT = "+it; output+="\nGross Salary of an Employee = "+gsal; JOptionPane.showMessageDialog(null,output); } public static void main(String args[]) { String sal,h,d,i; String ans=new String(); int sal1; sal= JOptionPane.showInputDialog(null,"Enter the salary"); sal1=Integer.parseInt(sal); salary3 employee=new salary3(sal1);

employee.calculation(); employee.printdata(); JOptionPane.showMessageDialog(null,"Good bye...."); System.exit(0); } } Program Name : SALARY~4 JAV (salary3.java) import javax.swing.*; public class salary3 { int s1; double hra,da,it,gsal; String output; salary3(int salary) { s1=salary;

} void calculation() { if (s1>=1000) { hra=(s1*10)/100; da=(s1*20)/100; it=0; } if(s1>1000 && s1<=3000) { hra=(s1*15)/100; da=(s1*25)/100; it=(s1*1.5)/100; } gsal=(s1+hra+da)-it; } void printdata() { output="Basic Salary of an Employee = "+s1; output+="\nHra = "+hra; output+="\nDa = "+da; output+="\nIT = "+it; output+="\nGross Salary of an Employee = "+gsal; JOptionPane.showMessageDialog(null,output); } public static void main(String args[]) { String sal,h,d,i; String ans=new String(); int sal1; sal= JOptionPane.showInputDialog(null,"Enter the salary"); sal1=Integer.parseInt(sal); salary3 employee=new salary3(sal1);

employee.calculation(); employee.printdata(); JOptionPane.showMessageDialog(null,"Good bye...."); System.exit(0); }

Topic : Inheritance
Program Name : DEMO~1 class person { String name; int ssno; JAV (demo.java)

04

person(String s,int a) { name=s;

ssno=a; } } class student extends person { String quali; student(String s,int a,String y) { super(s,a); quali=y; } public void printdata() { System.out.println("\n\t\tName = "+name); System.out.println("\n\t\tSocial sequrity no. = "+ssno); System.out.println("\n\t\tQualification = "+quali); } } public class demo { public static void main(String args[]) { student q=new student("Sourav",123,"B Sc."); q.printdata(); } } Program Name : DEMO2~2 import java.io.*; class person { String name; int ssno; person(String s,int a) { name=s; ssno=a; } } class student extends person { String quali; student(String s,int a,String y) { super(s,a); quali=y; } JAV (demo2.java)

10

class teacher extends student { int pub; teacher(String s,int a,String y,int pub1)

{ super(s,a,y); pub=pub1; } public void printdata() { System.out.println("\n\t\tName = "+name); System.out.println("\n\t\tSocial sequrity no. = "+ssno); System.out.println("\n\t\tQualification = "+quali); System.out.println("\n\t\tAnd year of publish = "+pub); } } public class demo2 { public static void main(String args[]) throws IOException { String mname=new String(); String scno=new String(); String qua=new String(); String publish=new String(); int secno,pub2; InputStreamReader sr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(sr); System.out.println("Enter the name"); mname=br.readLine(); System.out.println("Enter the security number"); scno=br.readLine(); System.out.println("Enter the qualification"); qua=br.readLine(); System.out.println("Enter the publish year"); publish=br.readLine(); pub2=Integer.parseInt(publish); secno=Integer.parseInt(scno); teacher q=new teacher(mname,secno,qua,pub2); q.printdata(); } } Program Name : FIGURE~1 JAV (figure.java) class figure1 { double dim1; double dim2; figure1(double a,double b) { dim1=a; dim2=b; } double area() { System.out.println("No area...."); return 0; }

11

class rectangle extends figure1

12
rectangle(double a,double b) { super(a,b); } double area() { return dim1*dim2; }

} public class figure { public static void main(String args[]) { figure1 f=new figure1(10,5); rectangle r=new rectangle(9,5); figure1 fg; fg=r; System.out.println ("Area is = "+fg.area()); fg=f; System.out.println ("Area is = "+fg.area()); } } Program Name : EXTEND~1 JAV (extendclass.java) import javax.swing.*; class staff { String type; staff(String type) { type=type; }

class teacher extends staff { String na,sub,type; teacher(String name,String subject,String t) { super(t); na=name; sub=subject; type=t; } void printdata() { JOptionPane.showMessageDialog(null,"Teacher type="+type); }

class student extends teacher { int marks; student(String name,String subject,String type,int m)

{ super(name,subject,type); marks=m; } void printdata() { JOptionPane.showMessageDialog(null,"Teacher name = "+na+ " subject name = "+sub+" marks = "+marks+" teacher type = "+type); } } public class extendclass { public static void main(String args[]) { student std=new student("Jayita","JAVA programming","Full time",80); std.printdata(); System.exit(0); } }

13

Topic : String

07

Program Name : STR~1 JAV (str.java) //This program demonstrates the string class constructors. import javax.swing.*; public class str { public static void main(String args[]) { char chararray[]={'b','i','r','t','h',' ','d','a','y'}; byte bytearray[]={(byte)'n',(byte)'e',(byte)'w',(byte)' ',(byte)'y',(byte)'e', (byte)'a',(byte)'r'}; //char bytearray[]={ 'n','e', 'w',' ', 'y', 'e', 'a', 'r'};

StringBuffer buffer; String s,s1,s2,s3,s4,s5,s6,s7,output; s=new String("Hello"); buffer=new StringBuffer("WELCOME TO JAVA"); //use the string constructors s1=new s2=new s3=new s4=new s5=new s6=new s7=new String(); String(s); String(chararray); String(chararray,6,3); String(bytearray,4,4); String(bytearray); String(buffer);

output="s1="+s1+"\ns2="+s2+"\ns3="+s3+"\ns4="+s4+"\ns5="+s5+"\ns6="+s6+"\ns7="+s7; JOptionPane.showMessageDialog(null,output,"Demonstrating String Class Constructors",JOptionPane.INFORMATION_MESSAGE); System.exit(0); }

14
Program Name : STR1~2 import java.io.*; import javax.swing.*; JAV (str1.java)

public class str1 { public static void main(String args[]) throws IOException { char abc[]; String s1,s2,output; String s3; s1=new String("TOTSOL computer centre"); s2=new String("22/2/2 Nim Chand Moitra Street"); s3=new String("A"); abc=new char[10]; output=new String(); int l=s1.length(); output+="\nThe length of the string is : "+l; output+="\n The string at possition 2 is "+s1.charAt(2); output+="\n The reverse string is : "; for(int i=l-1;i>=0;i--) { output+=s1.charAt(i); } output+="\n The ascii value of s3 = "+s3.hashCode(); output+="\n The character array is "; s1.getChars(0,9,abc,0); for(int i=0;i<abc.length;i++) { output+=abc[i]; } System.out.println(output); JOptionPane.showMessageDialog(null,output,"Display",JOptionPane.INFORMATION_MESSAGE); System.exit(0); }

Program Name : STRING~2 JAV (string1.java) import javax.swing.*; public class string1 { public static void main(String args[]) { String abc,output; output=new String(); char string2[]; abc=JOptionPane.showInputDialog("Enter the name"); int l=abc.length();

string2=new char[l]; JOptionPane.showMessageDialog(null,"The lenght of the name"+l); abc.getChars(0,l,string2,0); l=l-1; for(;l>=0;l--) { output+=abc.charAt(l); } JOptionPane.showMessageDialog(null,"The reverse of the name is "+output); output=""; for(int i=0;i<string2.length;i++) { output+="\n "+i+' '+string2[i]; } JOptionPane.showMessageDialog(null,"The array of the name is "+output); System.exit(0); }

15

Program Name : STRING~3 JAV (string2.java) // program for string function length,charAt,concat,indexOf,lastIndexOf,compareTo,equals,getChars import javax.swing.*; public class string2 { public static void main(String args[]) { String s,s1,output; char chararray[]; s=new String ("Welcome to TOTSOL"); s1=new String("22/2/2 Nim chand Moitra Street"); chararray=new char[7]; output="s :"+s; output+="\n Length of S :="+s.length(); output+="\n Characters of the array : "; for (int i=0; i<=s.length()-1; i++) output+=s.charAt(i) + " "; s.getChars(0,7,chararray,0); output+="\n The aharacter array is : "; for (int i=0; i<chararray.length; i++) output+= chararray[i]; output+="\nConcat s to s1 :="+s.concat(s1); output+="\nThe index of e is="+s.indexOf('e'); output+="\nThe index of e is="+s.indexOf('e',2); output+="\nThe lastindexof of e is="+s.lastIndexOf('e',2); output+="\nString compare s to s1"+s1.compareTo(s);

16
JOptionPane.showMessageDialog(null,output); System.exit(0); }

Program Name : STRING~4 JAV (stringcon.java) import javax.swing.*; public class stringcon { public static void main(String args[]) { char charArray[] = {'b','i','r','t','h',' ','d','a','y'}; byte byteArray[] = {(byte)'n',(byte)'e',(byte)'w',(byte)' ',(byte)'y',(byte)'e', (byte)'a',(byte)'r'}; StringBuffer buffer; String s, s1, s2, s3, s4, s5, s6, s7, output; s = new String( "Hello" ); buffer = new StringBuffer( "Welcome to Java Programming!" ); // use the String constructors s1 s2 s3 s4 s5 s6 s7 = = = = = = = new new new new new new new String(); String(s); String(charArray); String(charArray,6,3); String(byteArray,4,4); String(byteArray); String(buffer);

output = "s1 = "+s1+"\ns2 = "+s2+"\ns3 = "+s3+"\ns4 = "+s4+"\ns5 = "+s5+"\ns6 = "+s6+"\ns7 = "+s7; JOptionPane.showMessageDialog(null,output); System.exit(0); }

Program Name : SUBSTR~1 JAV (substring1.java) // substring import javax.swing.*; public class substring1 { public static void main(String args[]) { String letters="This is begining of Java Programming"; String output; // test substring methods output="The Main string is : "+letters+"\n"; output+="Substring from index 10 to end is : "; output+=letters.substring(10)+"\n"; output+="Substring from index 0 upto 6 is : "; output+=letters.substring(0,6); JOptionPane.showMessageDialog(null,output,"Demonstrating String Class Substring Method",JOptionPane.INFORMATION_MESSAGE);

17
System.exit(0); } JAV (str11.java)

Program Name : STR11~3 import javax.swing.*;

public class str11 { public static void main(String args[]) { String s,s1,output; int i,l=0; s=new String("Computer Center"); for (i=0; i<s.length(); i++) { if (s.charAt(i) ==' ') l=l+1; } output = "Length = "+(s.length()-l); JOptionPane.showMessageDialog(null,output); System.exit(0); }

Topic : Array
Program Name : INITAR~1 JAV (initarray.java) import javax.swing.*; public class initarray { public static void main(String args[]) { String output=""; int n[]={32,24,64,18,95,14,90,70,60,37}; output="Subscription value\n"; for(int i=0;i<n.length;i++) { output+=i+" }

03

"+n[i]+"\n";

JOptionPane.showMessageDialog(null,output); System.exit(0); } } Program Name : INITAR~2 JAV (initarray1.java) import javax.swing.*; public class initarray1 { public static void main(String args[])

{ String output=""; String s,p; int size; int n[]; p=JOptionPane.showInputDialog("Please enter the size of array"); size=Integer.parseInt(p); n=new int[size]; for(int i=0;i<size;i++) { s=JOptionPane.showInputDialog("Enter the value of array for pocket "+i); n[i]=Integer.parseInt(s); } for(int i=0;i<size;i++) { output+=i+" "+n[i]+"\n"; } JOptionPane.showMessageDialog(null,output); System.exit(0); } } Program Name : HISTOG~1 JAV (histogram.java) import javax.swing.*; public class histogram { public static void main(String args[]) { int n[]={19,3,15,7,11,9,13,5,17,1}; String output=""; output+="Element \t value \t Histogram"; for(int i=0;i<n.length;i++) { output+="\n"+i+"\t"+n[i]+"\t"; for (int j=1;j<=n[i];j++) { output+="*"; } } JOptionPane.showMessageDialog(null,output); System.exit(0); } }

18

Topic : Array object


Program Name : ARRAYO~1 JAV (arrayobj.java) import javax.swing.*; import java.io.*; class arrayobj {

01

String name; int rno; String rrno; public void getdata() { name=JOptionPane.showInputDialog("Enter name"); rrno=JOptionPane.showInputDialog("Enter rollno"); rno=Integer.parseInt(rrno); } public void printdata() { JOptionPane.showMessageDialog(null,"The name "+name+" and the roll number "+rno); } public static void main(String args[]) { arrayobj b1[]=new arrayobj[3]; int i; i=0; while(i<3) { b1[i]= new arrayobj(); b1[i].getdata(); i=i+1; } i=0; while(i<3) { b1[i].printdata(); i=i+1; } JOptionPane.showMessageDialog(null,"GOOD BYE"); System.exit(0); } }

19

Topic : Exception handling


Program Name : MYEXCE~1 JAV (myexception.java) import javax.swing.*; import java.text.*; public class myexception { public static void main (String args[]) { String number1,number2; int n1,n2,n3; try {

02

number1=JOptionPane.showInputDialog("Enter the first number"); number2=JOptionPane.showInputDialog("Enter the second number");

n1=Integer.parseInt(number1); n2=Integer.parseInt(number2); n3=n1/n2; } JOptionPane.showMessageDialog(null,"The result is "+n3);

20

catch(NumberFormatException nfe) { JOptionPane.showMessageDialog(null,"Allows only integer"); } catch(ArithmeticException dze) { JOptionPane.showMessageDialog(null,"Attempt to divide by zero"); } System.exit(0); } JAV (excep.java)

Program Name : EXCEP~1 import java.lang.*; import javax.swing.*;

public class excep { public static void main(String args[]) { String first,second; int num1,num2,result; first=JOptionPane.showInputDialog("Enter the number"); second=JOptionPane.showInputDialog("Enter the second number"); try {

num1=Integer.parseInt(first); num2=Integer.parseInt(second); result=num1/num2; JOptionPane.showMessageDialog(null,"The result is "+result);

} catch(ArithmeticException ae) { JOptionPane.showMessageDialog(null,"Invalid number"); } catch(NumberFormatException nfe) { JOptionPane.showMessageDialog(null,"Should be number"); } System.exit(0); } }

Topic : File handling


Program Name : TEXTFI~1 JAV import java.io.*; (textfile.java)

05

import import import import

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

21

public class textfile extends Applet implements ActionListener,ItemListener { Label name1; TextField name ; Choice mychoice ; Button b1,b2,b3; public void init() { name1=new Label("Name :",Label.RIGHT); name=new TextField("",20); mychoice =new Choice(); b1=new Button("Add"); b2=new Button("Open"); b3=new Button("Save"); mychoice.add("Select the item form the list...."); add(name1); add(name); add(b1); add(b2); add(b3); add(mychoice); mychoice.addItemListener(this); name.addActionListener(this); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); } public void actionPerformed(ActionEvent ae) { String s=(String) ae.getActionCommand(); if(s.equals("Add")) { String y=""; y=name.getText(); mychoice.add(y); name.setText(""); } if(s.equals("Save")) { String s1=""; s1=name.getText(); JOptionPane.showMessageDialog(null,s1); newfile(); } } public void itemStateChanged(ItemEvent ie) { }

22
public void newfile() { String s=""; FileWriter fw=new FileWriter("abc.txt"); InputStreamReader sr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(sr); s=br.readLine(); fw.write(s); fw.flush(); fw.close(); System.out.println(s); } (textfile1.java)

Program Name : TEXTFI~2 JAV import java.io.*;

public class textfile1 { public static void main (String args[]) throws IOException { String s=""; String salary; String filename; InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("Enter the file name.... "); filename=br.readLine(); FileWriter fw=new FileWriter(".\\"+filename); BufferedWriter output=new BufferedWriter(fw); FileReader fr=new FileReader(".\\"+filename); BufferedReader fr1=new BufferedReader(fr); System.out.println("Enter the name (to exit use qq).... "); s=br.readLine(); System.out.println("Enter the salary.... "); salary=br.readLine(); while(!s.equals("qq")) { output.write(s); output.write(" "); output.write(salary); output.newLine(); output.flush(); System.out.println("Enter the name.... "); s=br.readLine(); System.out.println("Enter the salary.... "); salary=br.readLine(); } fw.close(); System.out.println("Name Salary");

while((s=fr1.readLine())!=null) { System.out.println(s); } fr.close();

} (wtextfile.java)

23

Program Name : WTEXTF~1 JAV import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*;

public class wtextfile extends JFrame implements ActionListener,ItemListener { TextField text1; Choice combo1; Button b1,b2; wtextfile(String s) { super(s); Container c = getContentPane(); c.setLayout(new FlowLayout()); text1=new TextField("",20); combo1=new Choice(); combo1.add("Select the item.... "); b1=new Button("Add"); b2=new Button("Save"); c.add(text1); c.add(combo1); c.add(b1); c.add(b2); text1.addActionListener(this); b1.addActionListener(this); b2.addActionListener(this); combo1.addItemListener(this); } public void actionPerformed(ActionEvent ae) { String s=""; s=ae.getActionCommand(); if (s.equals("Add")) { s=text1.getText(); combo1.add(s); text1.setText(""); } if (s.equals("Save")) { int i=combo1.getItemCount(); try { FileWriter fw=new FileWriter(".\\mytextfile.txt"); BufferedWriter output=new BufferedWriter(fw); for (int y=0;y<i;y++) { output.write(combo1.getItem(y)); output.newLine(); output.flush(); } fw.close();

} }

}catch(IOException ie) { }

24

public void itemStateChanged(ItemEvent ie) { } public static void main(String args[]) throws IOException { wtextfile f=new wtextfile("window"); f.addWindowListener( new WindowAdapter() { public void WindowClosing(WindowEvent we) { System.exit(0); } } ); f.setSize(400,140); f.show(); } } Program Name : WTEXTF~2 JAV import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; (wtextfile1.java)

public class wtextfile1 extends JFrame implements ActionListener,ItemListener { TextField text1,text2; Choice combo1,combo2; Button b1,b2,b3; wtextfile1(String s) { super(s); Container c = getContentPane(); c.setLayout(new FlowLayout()); text1=new TextField("",20); combo1=new Choice(); combo1.add("Select the item.... "); text2=new TextField("",10); combo2=new Choice(); combo2.add("Select the next item.... "); b1=new Button("Add"); b2=new Button("Save"); b3=new Button("Open"); c.add(text1); c.add(text2); c.add(combo1); c.add(combo2);

25
c.add(b1); c.add(b2); c.add(b3); text1.addActionListener(this); text2.addActionListener(this); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); combo1.addItemListener(this); combo2.addItemListener(this); } public void actionPerformed(ActionEvent ae) { String s1,s=""; s=ae.getActionCommand(); if (s.equals("Add")) { s=text1.getText(); s1=text2.getText(); combo1.add(s); combo2.add(s1); text1.setText(""); text2.setText(""); } if (s.equals("Save")) { int i=combo1.getItemCount(); try { FileWriter fw=new FileWriter(".\\mytextfile.txt"); BufferedWriter output=new BufferedWriter(fw); for (int y=0;y<i;y++) { output.write(combo1.getItem(y)); output.write(" "); //output.write("12"); output.write(combo2.getItem(y)); output.newLine(); output.flush(); } fw.close(); }catch(IOException ie) { }

if (s.equals("Open")) { try { FileReader fr=new FileReader(".\\mytextfile.txt"); BufferedReader output=new BufferedReader(fr); String mys;

String mys1=""; while((mys=output.readLine())!=null) { mys1=""; int i=mys.length(); for(int j=0;j<i;j++) { if(mys.charAt(j)==' ') break; else { mys1+=mys.charAt(j); } } combo1.add(mys1); } fr.close(); }catch(IOException ie) { } } }

26

public void itemStateChanged(ItemEvent ie) { } public static void main(String args[]) throws IOException { wtextfile1 f=new wtextfile1("window"); f.addWindowListener( new WindowAdapter() { public void WindowClosing(WindowEvent we) { System.exit(0); } } ); f.setSize(400,140); f.show(); } (wtextfile11.java)

Program Name : WTEXTF~3 JAV import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*;

public class wtextfile11 extends JFrame implements ActionListener,ItemListener { TextField text1,text2; Choice combo1,combo2; Button b1,b2; wtextfile11(String s)

{ super(s); Container c = getContentPane(); c.setLayout(new FlowLayout()); text1=new TextField("",20); combo1=new Choice(); combo1.add("Select the item.... "); text2=new TextField("",10); combo2=new Choice(); combo2.add("Select the next item"); b1=new Button("Add"); b2=new Button("Save"); c.add(text1); c.add(text2); c.add(combo1); c.add(combo2); c.add(b1); c.add(b2); text1.addActionListener(this); text2.addActionListener(this); b1.addActionListener(this); b2.addActionListener(this); combo1.addItemListener(this); combo2.addItemListener(this); } public void actionPerformed(ActionEvent ae) { String s1,s=""; s=ae.getActionCommand(); if (s.equals("Add")) { s=text1.getText(); s1=text2.getText(); combo1.add(s); combo2.add(s1); text1.setText(""); text2.setText(""); } if (s.equals("Save")) { int i=combo1.getItemCount(); try { FileWriter fw=new FileWriter(".\\mytextfile.txt"); BufferedWriter output=new BufferedWriter(fw); for (int y=0;y<i;y++) { output.write(combo1.getItem(y));

27

output.write(" "); //output.write("12"); output.write(combo2.getItem(y)); output.newLine(); output.flush();

28

} }

fw.close(); }catch(IOException ie) { }

public void itemStateChanged(ItemEvent ie) { } public static void main(String args[]) throws IOException { wtextfile11 f=new wtextfile11("window"); f.addWindowListener( new WindowAdapter() { public void WindowClosing(WindowEvent we) { System.exit(0); } } ); f.setSize(400,140); f.show(); } }

Topic : Thread
Program Name : MULTIT~1 JAV import java.io.*; (multithread.java)

08

class newthread implements Runnable { String name; Thread t; newthread(String threadname) { name=threadname; t=new Thread(this,name); System.out.println("New thread "+t); t.start(); } public void run() { try { for(int i=0;i<5;i++) { System.out.println(name+" : "+ i); Thread.sleep(1000);

} }catch(InterruptedException ie) { System.out.println(name+" interrupted"); } System.out.println(name+" exiting"); } } public class multithread { public static void main(String args[]) { new newthread("t1"); new newthread("t2"); new newthread("t3"); try { Thread.sleep(1000); }catch(InterruptedException ie) { System.out.println("In the catch block"); } System.out.println("Exiting main thread"); } } Program Name : NEWTHR~1 JAV (newthread.java) class mythread implements Runnable { Thread t; //creating the thread object mythread() //constructor { t=new Thread(this,"Demo thread"); //define the thread object passing this //to call the run method of this object,name of the thread System.out.println("Child thread "+t); t.start(); //to strat the thread } public void run() { try { for(int i=0;i<5;i++) { System.out.println("Child Thread "+i); Thread.sleep(1000); } }catch(InterruptedException ie) { } System.out.println("Exiting child thread "); }

29

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

{ new mythread(); try {

30

for(int i=0;i<5;i++) { System.out.println("Main thread "+i); Thread.sleep(2000); } }catch(InterruptedException ie) { System.out.println("Error !!!"); }

System.out.println("Exiting the main thread"); }

Program Name : NEWTHR~2 JAV (newthread1.java) class mythread extends Thread { mythread() //constructor { super("Demo thread"); System.out.println("Child thread"); start(); //to strat the thread } public void run() { try { for(int i=0;i<5;i++) { System.out.println("Child Thread "); Thread.sleep(1000); } }catch(InterruptedException ie) { } System.out.println("Exiting child thread"); } } public class newthread1 { public static void main(String args[]) { new mythread(); try { for(int i=0;i<5;i++) { System.out.println("Main thread -> "+i); Thread.sleep(2000); } }catch(InterruptedException ie) { System.out.println("Error !!!");

} System.out.println("Exiting the main thread"); } } Program Name : NEWTHR~3 JAV (newthread2.java) class mythread extends Thread { mythread() //constructor { super("Demo thread"); System.out.println("Child thread"); start(); //to strat the thread } public void run() { try { for(int i=0;i<5;i++) { System.out.println("Child Thread "); Thread.sleep(1000); } }catch(InterruptedException ie) { } System.out.println("Exiting child thread"); }

31

public class newthread2 { public static void main(String args[]) { new mythread(); try { for(int i=0;i<5;i++) { System.out.println("Main thread "+i); Thread.sleep(2000); } }catch(InterruptedException ie) { System.out.println("Error !!!"); } System.out.println("Exiting the main thread"); } } Program Name : THREAD~1 JAV import java.io.*; (threadsuspend.java)

class newthread implements Runnable { String name; Thread t; boolean flag;

32
newthread(String threadname) { name=threadname; t=new Thread(this,name); System.out.println("New thread "+t); flag=false; t.start(); } public void run() { try { for(int i=0;i<5;i++) { System.out.println(name+" :"+ i); Thread.sleep(1000); synchronized(this) { while(flag) { wait(); } } } }catch(InterruptedException ie) { System.out.println(name +" interrupted "); } System.out.println(name+" exiting"); } public void mysuspend() { flag=true; } synchronized void myresume() { flag=false; notify(); } } public class threadsuspend { public static void main(String args[]) { newthread ob1= new newthread("t1"); newthread ob2=new newthread("t2"); try { Thread.sleep(1000); ob1.mysuspend(); System.out.println("Suspending thread t1"); Thread.sleep(1000); ob1.myresume(); System.out.println("Resuming thread t1"); }catch(InterruptedException ie) {

} try {

System.out.println("aaa");

33

System.out.println("Wating for thread to finish"); ob1.t.join(); }catch(InterruptedException ie) { System.out.println("aaa"); } System.out.println("Exiting main thread"); }

Program Name : THREAD~2 JAV (thread.java) public class thread { public static void main(String args[]) { Thread t=Thread.currentThread(); System.err.println("Current thread name "+t); t.setName("My Thread"); System.err.println("After the name change "+t); try { for(int i=0;i<5;i++) { System.out.println(i); Thread.sleep(1000); } }catch(InterruptedException ie) { System.err.println("Main thread interrupted"); } } } Program Name : THREAD~3 JAV (thread1.java) class newthread implements Runnable { Thread t; newthread() { t=new Thread(this,"Demo thread"); System.out.println("Child Thread "+t); t.start(); } public void run() { try { for(int i=5;i>=0;i--) { System.out.println("Child thread "+i); Thread.sleep(5000); } }catch(InterruptedException ie) { System.out.println("Child interuppted");

} System.out.println("Exit child thread"); } } public class thread1 { public static void main(String args[]) { new newthread(); try { for(int i=0;i<=5;i++) { System.out.println("Main Thread "+i); Thread.sleep(1000); } }catch(InterruptedException ie) { System.out.println("Main thread interrupted"); } System.out.println("Main thread exit"); } } Program Name : THREAD~4 JAV (thread2.java) class newthread extends Thread { Thread t; newthread() { super("Demo thread"); System.out.println("Child Thread "+this);/* reprsenting the current thread*/ start(); } public void run() { try { for(int i=5;i>=0;i--) { System.out.println("Child thread "+i); Thread.sleep(5000); } }catch(InterruptedException ie) { System.out.println("Child interuppted"); } System.out.println("Exit child thread"); } } public class thread2 { public static void main(String args[]) {

34

new newthread(); try { for(int i=0;i<=5;i++) { System.out.println("Main Thread "+i); Thread.sleep(1000); } }catch(InterruptedException ie) { System.out.println("Main thread interrupted"); } System.out.println("Main thread exit"); } }

35

Topic : Applet
Program Name : NERVOU~1 JAV import java.awt.*; import java.applet.*; (NervousText1.java)

16

public class NervousText1 extends Applet implements Runnable { String banner; // The text to be displayed char bannerChars[]; // The same text as an array of characters Thread runner; // The thread that is displaying the text public void init() { banner = getParameter("text"); if (banner == null) { banner = "HotJava"; } int bannerLength = banner.length(); bannerChars = new char[bannerLength]; banner.getChars(0, banner.length(), bannerChars, 0); setFont(new Font("TimesRoman", Font.BOLD, 36)); } public void start() { runner = new Thread(this); runner.start(); } public void run() { Thread me = Thread.currentThread(); while (runner == me) { try { Thread.sleep(10); repaint(); }

} }

catch (InterruptedException e) { }

36

public void paint(Graphics g) { for(int i=0; i<bannerChars.length; i++) { int x = (int) (10*Math.random() + 15*i); int y = (int) (10*Math.random() + 36); g.drawChars(bannerChars, i, 1, x, y); } } (applet2.java)

Program Name : APPLET~1 JAV import java.awt.*; import java.applet.*;

public class applet2 extends Applet { public void paint(Graphics g) { g.drawString("Welcome to java Applet",20,20); g.drawLine(5,30,50,30); g.setColor(Color.blue); g.drawRect(100,40,90,55); g.setColor(Color.red); g.fillRect( 200,40,90,55); g.fillOval(50,30,50,30); g.fillOval(120,130,56,60); g.draw3DRect(300,40,90,55,true); } } Program Name : APPLET~2 JAV import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; (appletgui.java)

public class appletgui1 extends Applet implements ActionListener,ItemListener { String msg=""; String s1 =""; String s2 =""; String s3 =""; String s4 =""; String s5 =""; Button yes,no,cancel; // button for creating command button Checkbox c1,c2,c3; // for creating check box Choice os,browser,mycombo; // for creating the combo box List l1,l2; // for creating list box Label name,password; // for creating label static TextField name1,pass; // for creating the text box TextArea myarea; // for creating multiline text box public void init() {

myarea=new TextArea(10,10); name=new Label("Name : ",Label.RIGHT); password=new Label("Password : ",Label.RIGHT); name1=new TextField(20); pass=new TextField("",10); pass.setEchoChar('*'); add(name); add(name1); add(password); add(pass); name1.addActionListener(this); pass.addActionListener(this); yes=new Button("Yes"); no=new Button("No"); cancel=new Button("Cancel"); c1=new Checkbox("Windows 98 ",true); c2=new Checkbox("Windows nt "); c3=new Checkbox("Dos "); os=new Choice(); mycombo=new Choice(); browser=new Choice(); l1=new List(2); l2=new List(2); l1.add("Windows 98 "); l1.add("Windows NT "); l1.add("Solaris "); l1.add("Macos "); l2.add("Internet Explorer "); // to add the item in combo box l2.add("Netsacpe 1.2x "); os.add("Windows 98 ");// to add the item in combo box os.add("Windows NT "); os.add("Solaris "); os.add("Macos "); browser.add("Internet Explorer "); // to add the item in combo box browser.add("Netsacpe 1.2x "); add(l1); add(l2); add(os); // to add the gui component in applet add(browser); add(mycombo); add(c1); add(c2); add(c3); add(yes); add(no); add(cancel); add(myarea); l1.addActionListener(this); l2.addActionListener(this); c1.addItemListener(this); c2.addItemListener(this); c3.addItemListener(this); os.addItemListener(this); browser.addItemListener(this); mycombo.addItemListener(this); yes.addActionListener(this); no.addActionListener(this); cancel.addActionListener(this); //myarea.addActionListener(this); } public void actionPerformed(ActionEvent ae)

37

{ String s=ae.getActionCommand(); if(s.equals("Yes")) { msg="U have pressed yes button "; } if(s.equals("No")) { msg="U have pressed no button"; } if(s.equals("Cancel")) { msg="U have pressed Cancel button"; } repaint(); } public void itemStateChanged(ItemEvent ie) { repaint(); }

38

public void paint(Graphics g) { g.drawString(msg,100,100); s1="Windows 98 "+c1.getState(); g.drawString(s1,200,200); s1="Windows 95 "+c2.getState(); g.drawString(s1,200,250); s1="Dos "+c3.getState(); g.drawString(s1,200,300); s2="The current os = "+os.getSelectedItem() + "browser = "+browser.getSelectedItem(); g.drawString(s2,200,350); s3="The current os = "+l1.getSelectedItem() + "browser = "+l2.getSelectedItem(); g.drawString(s3,200,400); s4="The name = "+name1.getText()+" and password = "+pass.getText(); g.drawString(s4,200,450); s5="Comment = "+myarea.getText(); g.drawString(s5,200,475); } } Program Name : APPLET~3 JAV import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; (appletgui1.java)

public class appletgui1 extends Applet implements ActionListener,ItemListener { String msg=""; String s1 =""; String s2 =""; String s3 =""; String s4 =""; String s5 =""; Button yes,no,cancel; // button for creating command button Checkbox c1,c2,c3; // for creating check box

Choice os,browser,mycombo; List l1,l2; Label name,password; TextField name1,pass; TextArea myarea;

// // // // //

for for for for for

creating creating creating creating creating

the combo box list box label static the text box multiline text box

39

public void init() { myarea=new TextArea(10,10); name=new Label("Name : ",Label.RIGHT); password=new Label("Password : ",Label.RIGHT); name1=new TextField(20); pass=new TextField("",10); pass.setEchoChar('*'); add(name); add(name1); add(password); add(pass); name1.addActionListener(this); pass.addActionListener(this); yes=new Button("Yes"); no=new Button("No"); cancel=new Button("Cancel"); c1=new Checkbox("Windows 98 ",true); c2=new Checkbox("Windows nt "); c3=new Checkbox("Dos "); os=new Choice(); mycombo=new Choice(); browser=new Choice(); l1=new List(2); l2=new List(2); l1.add("Windows 98 "); l1.add("Windows NT "); l1.add("Solaris "); l1.add("Macos "); l2.add("Internet Explorer "); // to add the item in combo box l2.add("Netsacpe 1.2x "); os.add("Windows 98 ");// to add the item in combo box os.add("Windows NT "); os.add("Solaris "); os.add("Macos "); browser.add("Internet Explorer "); // to add the item in combo box browser.add("Netsacpe 1.2x "); add(l1); add(l2); add(os); // to add the gui component in applet add(browser); add(mycombo); add(c1); add(c2); add(c3); add(yes); add(no); add(cancel); add(myarea); l1.addActionListener(this); l2.addActionListener(this); c1.addItemListener(this); c2.addItemListener(this); c3.addItemListener(this); os.addItemListener(this); browser.addItemListener(this);

mycombo.addItemListener(this); yes.addActionListener(this); no.addActionListener(this); cancel.addActionListener(this); //myarea.addActionListener(this); } public void actionPerformed(ActionEvent ae) { String s=ae.getActionCommand(); if(s.equals("Yes")) { msg="U have pressed yes button "; } if(s.equals("No")) { msg="U have pressed no button"; } if(s.equals("Cancel")) { msg="U have pressed Cancel button"; } repaint(); } public void itemStateChanged(ItemEvent ie) { repaint(); }

40

public void paint(Graphics g) { g.drawString(msg,100,100); s1="Windows 98 "+c1.getState(); g.drawString(s1,200,200); s1="Windows 95 "+c2.getState(); g.drawString(s1,200,250); s1="Dos "+c3.getState(); g.drawString(s1,200,300); s2="The current os = "+os.getSelectedItem() + "browser = "+browser.getSelectedItem(); g.drawString(s2,200,350); s3="The current os = "+l1.getSelectedItem() + "browser = "+l2.getSelectedItem(); g.drawString(s3,200,400); s4="The name = "+name1.getText()+" and password = "+pass.getText(); g.drawString(s4,200,450); s5="Comment = "+myarea.getText(); g.drawString(s5,200,475); } } Program Name : APPLET~4 JAV import java.awt.*; import java.awt.event.*; import java.applet.*; import java.io.*; (applettext.java)

public class applettext extends Applet implements Runnable,MouseListener {

String s="Welcome to the world of java applet. "; Thread mythread=null; boolean y,y1; Font f1=new Font("Arial Black",3,50); public void init() { y=true; y1=false; setBackground(Color.blue); setForeground(Color.orange); setFont(f1); addMouseListener(this); } public void start() { y1=false; mythread=new Thread(this); mythread.start(); }

41

//go to run

public void run() { char ch; try { for(;;) { repaint(); //go to paint Thread.sleep(100); ch=s.charAt(0); s=s.substring(1,s.length()); s+=ch; if(y==false) { y1=true; break; } } } catch(InterruptedException ie) { } } public void paint(Graphics g) { g.drawString(s,100,100); } public void mouseClicked(MouseEvent me) { y=false; if (y1==true) { y=true; start(); } }

public void mouseEntered(MouseEvent me) { showStatus("The mouse pointer has entered.... "); } public void mouseReleased(MouseEvent me) { showStatus("The mouse pointer has released...."); } public void mouseExited(MouseEvent me) { showStatus("The mouse pointer has exited.... }

42

");

public void mousePressed(MouseEvent me) { showStatus("The mouse pointer has pressed.... "); } (applet1.java)

Program Name : APPLET~5 JAV import java.applet.*; import java.awt.*;

public class applet1 extends Applet { String msg=""; public void init() { setBackground(Color.red); setForeground(Color.blue); msg=msg+" Applet initialising "; } public void paint(Graphics g) { msg+=" Entering into paint "; g.drawString(msg,40,50); } public void start() { msg+=" Applet started "; } } Program Name : APPLET~6 JAV import java.applet.*; import java.awt.*; import java.awt.event.*; (applet3.java)

public class applet3 extends Applet implements Runnable,MouseListener { String msg="Welcome to the World Of Java"; String msg1="Java a Cup of Cofee"; Thread t=null; boolean flag,flag1; public void init() {

flag=false; flag1=true; t=new Thread(this); setBackground(Color.red); setForeground(Color.blue); addMouseListener(this); } public void start() { flag1=true; t=new Thread(this); t.start(); } public void run() { char ch; for(;;) { try { repaint(); Thread.sleep(500); ch=msg.charAt(0); msg=msg.substring(1,msg.length()); msg+=ch; if (flag) { flag1=false; break; } } catch(InterruptedException ie) { } } } public void paint(Graphics g) { g.setColor(Color.blue); g.drawString(msg,40,50); } public void mouseClicked(MouseEvent me) { flag=true; if(flag1==false) { flag=false; start(); } } public void mouseEntered(MouseEvent me) { showStatus("The mouse pointer has entered.... "); } public void mouseReleased(MouseEvent me)

43

{ showStatus("The mouse pointer has released...."); } public void mouseExited(MouseEvent me) { showStatus("The mouse pointer has exited.... }

44

");

public void mousePressed(MouseEvent me) { showStatus("The mouse pointer has pressed.... "); } (applet4.java)

Program Name : APPLET~7 JAV import java.awt.*; import java.applet.*;

public class applet4 extends Applet implements Runnable { String banner; // The text to be displayed char bannerChars[]; // The same text as an array of characters Thread runner; // The thread that is displaying the text public void init() { banner = getParameter("text"); if (banner == null) { banner = "H o t J a v a"; } int bannerLength = banner.length(); bannerChars = new char[bannerLength]; banner.getChars(0, banner.length(), bannerChars, 0); setFont(new Font("TimesRoman", Font.BOLD, 36)); } public void start() { runner = new Thread(this); runner.start(); } public void run() { Thread me = Thread.currentThread(); while (runner == me) { try { setBackground(new Color((int)(Math.random()*256),(int)(Math.random()*256),(int) (Math.random()*256))); Thread.sleep(1000); repaint(); } catch (InterruptedException e) { }

45

public void paint(Graphics g) { for(int i=0;i<bannerChars.length;i++) { int x = (int) (50*Math.random() + 35*i); int y = (int) (50*Math.random() + 36); g.setColor(new Color((int)(Math.random()*256),(int)(Math.random()*256),(int) (Math.random()*256))); g.drawChars(bannerChars, i, 1, x, y); } } } Program Name : APPLET~8 JAV import java.applet.*; import java.awt.*; (applet22.java)

public class applet22 extends Applet implements Runnable { String msg="Welcome to the World Of Java"; String msg1="Java a Cup of Cofee"; Thread t=null; Thread t1=null; public void init() { t=new Thread(this); t1=new Thread(this); setBackground(Color.red); setForeground(Color.blue); } public void start() { t.start(); t1.start(); } public void run() { char ch; for(;;) { try { repaint(); Thread.sleep(500); ch=msg.charAt(0); msg=msg.substring(1,msg.length()); msg+=ch; ch=msg1.charAt(0); msg1=msg1.substring(1,msg1.length()); msg1+=ch; } catch(InterruptedException ie) { } } }

46
public void paint(Graphics g) { g.setColor(Color.blue); g.drawString(msg,40,50); g.setColor(Color.green); g.drawString(msg1,40,90); } } Program Name : IMAGEM~2 JAV import java.awt.*; import java.awt.event.*; import javax.swing.*; (imagem.java)

public class imagem extends JApplet { private ImageIcon mapimage; private int width,height; public void init() { addMouseListener(new MouseAdapter() { public void mouseExited(MouseEvent e) { showStatus("pointer outside applet"); } } ); addMouseMotionListener(new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { showStatus(translateLocation(e.getX())); } } ); mapimage=new ImageIcon("CLOUD.GIF"); width=mapimage.getIconWidth(); height=mapimage.getIconHeight(); setSize(width,height); } public void paint(Graphics g) { mapimage.paintIcon(this,g,0,0); } public String translateLocation(int x) { int iconwidth=width/6; if(x>=0 && x<=iconwidth) return "common programming error"; else if(x>iconwidth*2 && x<=iconwidth*2) return "good";

else if(x>iconwidth*3 && x<=iconwidth) return "performance"; return""; } (Clock2.java)

47

Program Name : CLOCK2~1 JAV import java.util.*; import java.awt.*; import java.applet.*; import java.text.*;

public class Clock2 extends Applet implements Runnable { Thread timer; // The thread that displays clock int lastxs, lastys, lastxm, lastym, lastxh, lastyh; // Dimensions used to draw hands SimpleDateFormat formatter; // Formats the date displayed String lastdate; // String to hold date displayed Font clockFaceFont; // Font for number display on clock Date currentDate; // Used to get date to display Color handColor; // Color of main hands and dial Color numberColor; // Color of second hand and numbers public void init() { int x,y; lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0; formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy", Locale.getDefault()); currentDate = new Date(); lastdate = formatter.format(currentDate); clockFaceFont = new Font("Serif", Font.PLAIN, 14); handColor = Color.blue; numberColor = Color.darkGray; try { setBackground(new Color(Integer.parseInt(getParameter("bgcolor"),16))); } catch (Exception E) { } try { handColor = new Color(Integer.parseInt(getParameter("fgcolor1"),16)); } catch (Exception E) { } try { numberColor = new Color(Integer.parseInt(getParameter("fgcolor2"),16)); } catch (Exception E) { } resize(300,300); // Set clock window size } // Plotpoints allows calculation to only cover 45 degrees of the circle, // and then mirror public void plotpoints(int x0, int y0, int x, int y, Graphics g) { g.drawLine(x0+x,y0+y,x0+x,y0+y); g.drawLine(x0+y,y0+x,x0+y,y0+x); g.drawLine(x0+y,y0-x,x0+y,y0-x); g.drawLine(x0+x,y0-y,x0+x,y0-y); g.drawLine(x0-x,y0-y,x0-x,y0-y); g.drawLine(x0-y,y0-x,x0-y,y0-x); g.drawLine(x0-y,y0+x,x0-y,y0+x); g.drawLine(x0-x,y0+y,x0-x,y0+y); }

// Circle is just Bresenham's algorithm for a scan converted circle public void circle(int x0, int y0, int r, Graphics g) { int x,y; float d; x=0; y=r; d=5/4-r; plotpoints(x0,y0,x,y,g); while (y>x) { if (d<0) { d=d+2*x+3; x++; } else { d=d+2*(x-y)+5; x++; y--; } } } // Paint is the main part of the program public void paint(Graphics g) { int xh, yh, xm, ym, xs, ys, s = 0, m = 10, h = 10, xcenter, ycenter; String today; currentDate = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("s",Locale.getDefault()); try { s = Integer.parseInt(formatter.format(currentDate)); } catch (NumberFormatException n) { s = 0; } formatter.applyPattern("m"); try { m = Integer.parseInt(formatter.format(currentDate)); } catch (NumberFormatException n) { m = 10; } plotpoints(x0,y0,x,y,g);

48

formatter.applyPattern("h"); try { h = Integer.parseInt(formatter.format(currentDate)); } catch (NumberFormatException n) { h = 10; } formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy"); today = formatter.format(currentDate); xcenter=80;

ycenter=55; // a= s* pi/2 - pi/2 (to switch 0,0 from 3:00 to 12:00) // x = r(cos a) + xcenter, y = r(sin a) + ycenter xs ys xm ym xh yh = = = = = = (int)(Math.cos(s * 3.14f/30 (int)(Math.sin(s * 3.14f/30 (int)(Math.cos(m * 3.14f/30 (int)(Math.sin(m * 3.14f/30 (int)(Math.cos((h*30 + m/2) (int)(Math.sin((h*30 + m/2) * * 3.14f/2) * 45 + xcenter); 3.14f/2) * 45 + ycenter); 3.14f/2) * 40 + xcenter); 3.14f/2) * 40 + ycenter); 3.14f/180 - 3.14f/2) * 30 + xcenter); 3.14f/180 - 3.14f/2) * 30 + ycenter);

49

// Draw the circle and numbers g.setFont(clockFaceFont); g.setColor(handColor); circle(xcenter,ycenter,50,g); g.setColor(numberColor); g.drawString("9",xcenter-45,ycenter+3); g.drawString("3",xcenter+40,ycenter+3); g.drawString("12",xcenter-5,ycenter-37); g.drawString("6",xcenter-3,ycenter+45); // Erase if necessary, and redraw g.setColor(getBackground()); if (xs != lastxs || ys != lastys) { g.drawLine(xcenter, ycenter, lastxs, lastys); g.drawString(lastdate, 5, 125); } if (xm != lastxm || ym != lastym) { g.drawLine(xcenter, ycenter-1, lastxm, lastym); g.drawLine(xcenter-1, ycenter, lastxm, lastym); } if (xh != lastxh || yh != lastyh) { g.drawLine(xcenter, ycenter-1, lastxh, lastyh); g.drawLine(xcenter-1, ycenter, lastxh, lastyh); } g.setColor(numberColor); g.drawString("", 5, 125); g.drawString(today, 5, 125); g.drawLine(xcenter, ycenter, xs, ys); g.setColor(handColor); g.drawLine(xcenter, ycenter-1, xm, ym); g.drawLine(xcenter-1, ycenter, xm, ym); g.drawLine(xcenter, ycenter-1, xh, yh); g.drawLine(xcenter-1, ycenter, xh, yh); lastxs=xs; lastys=ys; lastxm=xm; lastym=ym; lastxh=xh; lastyh=yh; lastdate = today; currentDate=null; public void start()

{ timer = new Thread(this); timer.start(); } public void stop() { timer = null; } public void run() { Thread me = Thread.currentThread(); while (timer == me) { try { Thread.currentThread().sleep(100); } catch (InterruptedException e) { } repaint(); }

50

public void update(Graphics g) { paint(g); } public String getAppletInfo() { return "Title: A Clock \nAuthor: Rachel Gollub, 1995 \nAn analog clock."; } public String[][] getParameterInfo() { String[][] info = { {"bgcolor", "hexadecimal RGB number", "The background color. Default is the color of your browser."}, {"fgcolor1", "hexadecimal RGB number", "The color of the hands and dial. Default is blue."}, {"fgcolor2", "hexadecimal RGB number", "The color of the seconds hand and numbers. Default is dark gray."} }; return info; } } Program Name : AUDIO~1 JAV import java.applet.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; (audio.java)

public class audio extends JApplet { AudioClip sound1,sound2,currentsound; JButton playsound,loopsound,stopsound; JComboBox choosesound; public void init()

{ Container c=getContentPane(); c.setLayout(new FlowLayout()); String choices[]={"Welcome","H!"}; choosesound=new JComboBox(choices); choosesound.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ie) { currentsound.stop(); currentsound=choosesound.getSelectedIndex()==0 ? sound1:sound2; } }); c.add(choosesound); Buttonhandler handler=new Buttonhandler(); playsound=new JButton("Play"); playsound.addActionListener(handler); c.add(playsound); loopsound=new JButton("Loop"); loopsound.addActionListener(handler); c.add(loopsound); stopsound=new JButton("Stop"); stopsound.addActionListener(handler); c.add(stopsound); sound1=getAudioClip(getDocumentBase(),"WELCOM98.WAV"); System.out.println("aaaa"+sound1); sound2=getAudioClip(getDocumentBase(),"START.WAV"); currentsound=sound1; } public void stop() { currentsound.stop(); } public class Buttonhandler implements ActionListener { public void actionPerformed(ActionEvent ae) { if (ae.getSource()==playsound) currentsound.play(); else if (ae.getSource()==loopsound) currentsound.loop(); else if(ae.getSource()==stopsound) currentsound.stop(); } } } Program Name : GLAYOU~1 JAV (glayout.java) //run this program with "java glayout" command, not with appletviewer //after compilation with javac glayout.java command import java.awt.*; import java.awt.event.*; import javax.swing.*; public class glayout extends JFrame implements ActionListener { Button b1,b2,b3; Container c; GridLayout layout1;

51

52
public glayout() { super("GridLayout Demo"); layout1=new GridLayout(4,3); c=getContentPane(); c.setLayout(layout1); b1=new Button("Left"); b2=new Button("Centre"); b3=new Button("Right"); c.add(b1,BorderLayout.NORTH); c.add(b2,BorderLayout.SOUTH); c.add(b3,BorderLayout.EAST); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); } public void actionPerformed(ActionEvent ae) { } public static void main(String args[]) { glayout lay2=new glayout(); lay2.addWindowListener( new WindowAdapter() { public void WindowClosing(WindowEvent e) { System.exit(0); } } ); lay2.setSize(300,72); lay2.show(); } (gridlayout.java)

Program Name : GRIDLA~1 JAV import java.awt.*; import java.awt.event.*; import javax.swing.*;

public class gridlayout extends JFrame implements ActionListener { Label l1,l2; TextField t1,t2; Button b1,b2; JPanel buttonPanel; GridLayout grid1; Container c; public gridlayout() { super("Grid Layout Demo"); l1=new Label("Name"); l2=new Label("Age");

c=getContentPane(); buttonPanel=new JPanel(); buttonPanel.setLayout(new GridLayout(1,2)); t1=new TextField("",20); t2=new TextField("",20); b1=new Button("Add"); b2=new Button("Save"); c.add(l1); c.add(t1); c.add(l2); c.add(t2); buttonPanel.add(b1); buttonPanel.add(b2); c.add(buttonPanel,BorderLayout.SOUTH); setSize(300,150); show(); } public void actionPerformed(ActionEvent ae) { } public static void main(String aargs[]) { gridlayout app=new gridlayout(); app.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we) { System.exit(0); } }

53

); } }

Program Name : IMAGEMAP JAV import java.awt.*; import java.awt.event.*; import javax.swing.*;

(imagemap.java)

public class imagemap extends JApplet { private ImageIcon mapimage; private int width,height; public void init() { addMouseListener(new MouseAdapter() { public void mouseExited(MouseEvent e) {

showStatus("pointer outside applet"); } } ); addMouseMotionListener(new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { showStatus(translateLocation(e.getX())); } } ); mapimage=new ImageIcon("CLOUD.GIF"); width=mapimage.getIconWidth(); height=mapimage.getIconHeight(); setSize(width,height); } public void paint(Graphics g) { mapimage.paintIcon(this,g,0,0); } public String translateLocation(int x) { int iconwidth=width/6; if(x>=0 && x<=iconwidth) return "common programming error"; else if(x>iconwidth*2 && x<=iconwidth*2) return "good"; else if(x>iconwidth*3 && x<=iconwidth) return "performance"; return""; } } Program Name : IMAGE~1 import java.applet.*; import java.awt.*; import javax.swing.*; JAV (image.java)

54

public class image extends JApplet { Image logo1; ImageIcon logo2; public void init() { logo1=getImage(getDocumentBase(),"cloud.gif"); logo2=new ImageIcon("cloud.gif"); } public void paint(Graphics g) { g.drawImage(logo1,100,100,this); g.drawImage(logo1,0,120,getWidth(),getHeight()-120,this); logo2.paintIcon(this,g,180,0); }

55

Topic : Swing Topic : Event handling


Program Name : KEYBOA~1 JAV import java.io.*; import java.applet.*; import java.awt.*; import java.awt.event.*; (keyboard1.java)

---05

public class keyboard1 extends Applet implements KeyListener { String msg="Key board testing"; int x,y; public void init() { x=20; y=30; setBackground(Color.red); setForeground(Color.blue); addKeyListener(this); requestFocus(); } public void paint(Graphics g) { g.drawString(msg,x,y); } public void keyPressed(KeyEvent ke) { showStatus("Key pressed"); repaint(); } public void keyReleased(KeyEvent ke) { showStatus("Key released"); repaint(); } public void keyTyped(KeyEvent ke) { msg+=ke.getKeyChar(); repaint(); } } Program Name : MOUSE1~1 JAV import java.io.*; import java.applet.*; import java.awt.*; import java.awt.event.*; (mouse1.java)

public class mouse1 extends Applet implements MouseListener,MouseMotionListener { String msg=""; int x,y;

56
public void init() { x=20; y=30; setBackground(Color.red); setForeground(Color.blue); addMouseListener(this); addMouseMotionListener(this); } public void paint(Graphics g) { g.drawString(msg,x,y); } public void mouseClicked(MouseEvent me) { msg="Mouse Clicked"; repaint(); } public void mouseEntered(MouseEvent me) { msg="Mouse Entered"; repaint(); } public void mouseExited(MouseEvent me) { msg="Mouse Exited"; repaint(); } public void mousePressed(MouseEvent me) { msg="Mouse Pressed"; } public void mouseReleased(MouseEvent me) { msg="Mouse Released"; } public void mouseMoved(MouseEvent me) { msg="Mouse Move"; repaint(); } public void mouseDragged(MouseEvent me) { msg="Mouse Dragged"; x=me.getX(); y=me.getY(); repaint(); } } Program Name : KEYEVE~1 JAV import java.awt.*; (keyevent.java)

import java.awt.event.*; import java.applet.*; public class keyevent extends Applet implements KeyListener { String msg=""; int x=10,y=20; public void init() { addKeyListener(this); requestFocus(); // request input focus to the applet window } public void keyPressed(KeyEvent ke) { int key=ke.getKeyCode(); switch(key) { case KeyEvent.VK_F1: msg="Keypressed F1"; break; case KeyEvent.VK_F2: msg="Keypressed F2"; break; case 116: msg="Keypressed F6"; break;

57

msg+="The valuekey = "+key; repaint(); } public void keyReleased(KeyEvent ke) { showStatus("Key up"); } public void keyTyped(KeyEvent ke) { msg="Key typed "+ke.getKeyChar(); repaint(); } public void paint(Graphics g) { g.drawString(msg,x,y); } } Program Name : TEXTBO~1 JAV import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; (textbox.java)

public class textbox extends Applet implements ActionListener {

String msg=""; String s=""; String s1=""; Button yes,no,cancel; // button for creating command button Label name,password; // for creating label static TextField name1,pass; // for creating the text box TextArea myarea;//for creating multiline text box public void init() { myarea=new TextArea(10,10); //numbner of rows and columns name=new Label("Name : ",Label.RIGHT); password=new Label("Password : ",Label.RIGHT); name1=new TextField(20);//size of text box pass=new TextField("",10); pass.setEchoChar('*'); add(name); add(name1); add(password); add(pass); name1.addActionListener(this); pass.addActionListener(this); yes=new Button("Yes"); no=new Button("No"); cancel=new Button("Cancel"); add(yes); add(no); add(cancel); add(myarea); yes.addActionListener(this); no.addActionListener(this); cancel.addActionListener(this); //myarea.addActionListener(this); } public void actionPerformed(ActionEvent ae) { String s=ae.getActionCommand(); if(s.equals("Yes")) { msg="U have pressed yes button"; } if(s.equals("No") ) { msg="U have pressed no button"; } if(s.equals("Cancel") ) { msg="U have pressed Cancel button"; } repaint(); } public void paint(Graphics g) { s=name1.getText(); g.drawString(msg,100,400); g.drawString(s,400,400);

58

s1=pass.getText(); g.drawString(s1,500,400); } (mouseevent.java)

59

Program Name : MOUSEE~1 JAV import java.awt.*; import java.awt.event.*; import java.applet.*;

public class mouseevent extends Applet implements MouseListener,MouseMotionListener { String msg=" "; int mouseX=0,mouseY=0;//coordinate of mouse public void init() { addMouseListener(this); addMouseMotionListener(this); } //handle mouse click public void mouseClicked(MouseEvent me) { //save coordinate mouseX=0; mouseY=0; msg="Mouse Clicked. "; repaint(); } //handle mouse entered public void mouseEntered(MouseEvent me) { //save coordinates mouseX=0; mouseY=10; msg="Mouse Entered. "; repaint(); } //handle mouse exited. public void mouseExited(MouseEvent me) { //save coordinates mouseX=0; mouseY=10; msg="Mouse Exited. "; repaint(); } //handle mouse button pressed public void mousePressed(MouseEvent me) { //save coordinates mouseX=me.getX(); mouseY=me.getY(); msg="Mouse Pressed"; repaint(); } //handle button released public void mouseReleased(MouseEvent me) {

//save coordinate mouseX=me.getX(); mouseY=me.getY(); msg="Up"; repaint(); } //Handle mouse dragged. public void mouseDragged(MouseEvent me) { //save coordinates mouseX=me.getX(); mouseY=me.getY(); msg="Dragged"; showStatus("Dragging mouse at " + mouseX + ", " + mouseY); repaint(); } //handle mouse moved public void mouseMoved(MouseEvent me) { //show status showStatus("Moving mose at " + me.getX() + ", " +me.getY()); repaint(); } //Display msg in applet window at current x,y location. public void paint(Graphics g) { g.drawString(msg,mouseX,mouseY); }

60

Topic : Awt
Program Name : COMBOB~1 JAV import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; (combobox1.java)

06

public class combobox1 extends { String msg=""; String s1=""; String s2=""; String s3=""; String s4=""; String s5=""; Checkbox c1,c2,c3; //for Choice os,browser,mycombo; List l1,l2;// for creating

Applet implements ItemListener,ActionListener

creating check box //for creating the combo box list box

public void init() { c1=new Checkbox("Windows 98",true); c2=new Checkbox("Windows NT"); c3=new Checkbox("DOS"); os=new Choice(); mycombo=new Choice(); browser=new Choice(); l1=new List(2);

l2=new List(2); l1.add("Windows 98"); l1.add("Windows NT"); l1.add("Solaris"); l1.add("Macos"); l2.add("Internet Explorer"); // to add the item in combo box l2.add("Netsacpe 1.2x"); os.add("Windows 98");// to add the item in combo box os.add("Windows NT"); os.add("Solaris"); os.add("Macos"); browser.add("Internet Explorer"); // to add the item in combo box browser.add("Netsacpe 1.2x"); add(l1); add(l2); add(os); // to add the gui component in applet add(browser); add(mycombo); add(c1); add(c2); add(c3); l1.addActionListener(this); l2.addActionListener(this); c1.addItemListener(this); c2.addItemListener(this); c3.addItemListener(this); os.addItemListener(this); browser.addItemListener(this); mycombo.addItemListener(this); } public void actionPerformed(ActionEvent ie) { repaint(); } public void itemStateChanged(ItemEvent ie) { repaint(); }

61

public void paint(Graphics g) { s1="Windows 98 "+c1.getState(); g.drawString(s1,200,200); s1="Windows NT "+c2.getState(); g.drawString(s1,200,250); s1="Dos "+c3.getState(); g.drawString(s1,200,300); s2="The current OS = "+os.getSelectedItem() + " Browser = "+browser.getSelectedItem(); g.drawString(s2,200,350); s3="The current OS = "+l1.getSelectedItem() + " Browser = "+l2.getSelectedItem(); g.drawString(s3,200,400); } } Program Name : MENU~1 JAV import java.applet.*; import java.awt.*; import java.awt.event.*; class myframe extends Frame (menu.java)

62
String msg="U have clicked on"; myframe(String title) { super(title); TextField t; Button b1; setLayout(new FlowLayout(FlowLayout.LEFT)); MenuBar mbar=new MenuBar(); setMenuBar(mbar); MenuItem a1,a2,a3,a4; Menu file=new Menu("File"); file.add(a1=new MenuItem("New")); file.add(a2=new MenuItem("Save")); Menu edit=new Menu("Edit"); edit.add(a3=new MenuItem("Cut")); edit.add(a4=new MenuItem("Copy")); mbar.add(file); mbar.add(edit); t=new TextField("",20); b1=new Button("Command1"); add(t); add(b1); MyWindowClass adapter=new MyWindowClass(this); addWindowListener(adapter); mymenuhandler handler=new mymenuhandler(this); a1.addActionListener(handler); a2.addActionListener(handler); a3.addActionListener(handler); a4.addActionListener(handler); } public void paint(Graphics g) { g.drawString(msg,100,200); }

class MyWindowClass extends WindowAdapter { myframe f1; public MyWindowClass(myframe f2) { this.f1=f2; } public void windowClosing(WindowEvent we) { f1.setVisible(false); } } class mymenuhandler implements ActionListener { myframe f1; public mymenuhandler(myframe f2) { this.f1=f2; }

63
public void actionPerformed(ActionEvent ae) { String msg="You have selected"; String s=(String) ae.getActionCommand(); if(s.equals("new")) { msg="Open program for new"; } f1.msg=msg; f1.repaint(); } } public class menu extends Applet { Frame f; public void init() { f=new myframe("Menu"); f.setVisible(true); } public void start() { f.setVisible(true); } public void stop() { f.setVisible(false); } (blayout.java)

Program Name : BLAYOU~1 JAV import java.awt.*; import java.awt.event.*; import javax.swing.*;

public class blayout extends JFrame implements ActionListener { Button b1,b2,b3; Container c; BorderLayout layout1; public blayout() { super("BorderLayout Demo"); layout1=new BorderLayout(); c=getContentPane(); c.setLayout(layout1); b1=new Button("Left"); b2=new Button("Centre"); b3=new Button("Right"); c.add(b1,BorderLayout.NORTH); c.add(b2,BorderLayout.SOUTH);

c.add(b3,BorderLayout.EAST); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); } public void actionPerformed(ActionEvent ae) { } public static void main(String args[]) { blayout lay2=new blayout(); lay2.addWindowListener( new WindowAdapter() { public void WindowClosing(WindowEvent e) { System.exit(0); } } ); lay2.setSize(300,72); lay2.show(); } (layout.java)

64

Program Name : LAYOUT~1 JAV import java.awt.*; import java.awt.event.*; import javax.swing.*;

public class layout extends JFrame implements ActionListener { Button b1,b2,b3; Container c; FlowLayout layout1; public layout() { super("FlowLayout demo"); layout1=new FlowLayout(); c=getContentPane(); c.setLayout(layout1); b1=new Button("Left"); b2=new Button("Centre"); b3=new Button("Right"); c.add(b1); c.add(b2); c.add(b3); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); } public void actionPerformed(ActionEvent ae) { String s; s=ae.getActionCommand();

65
if(s.equals("Centre")) { layout1.setAlignment(FlowLayout.CENTER); setSize(300,72); show(); } if(s.equals("Left")) { layout1.setAlignment(FlowLayout.LEFT); setSize(300,72); show(); } if(s.equals("Right")) { layout1.setAlignment(FlowLayout.RIGHT); setSize(300,72); show(); } } public static void main(String args[]) { layout lay2=new layout(); lay2.addWindowListener( new WindowAdapter() { public void WindowClosing(WindowEvent e) { System.exit(0); } } ); lay2.setSize(300,72); lay2.show(); } } Program Name : MENU1~2 JAV import java.awt.*; import java.awt.event.*; import java.applet.*; (menu1.java)

class sampleframe extends Frame { String msg="My Menu"; sampleframe(String title) { super(title); MenuBar mbar=new MenuBar(); setMenuBar(mbar); Menu file=new Menu("File"); Menu format=new Menu("Format"); Menu color=new Menu("Color"); MenuItem item1,item2,item3,item4; CheckboxMenuItem item5,item6; item5=new CheckboxMenuItem("Bold"); item6=new CheckboxMenuItem("Italic"); color.add(item5); color.add(item6); file.add(item1=new MenuItem("New")); file.add(item2=new MenuItem("Open"));

color.add(item3=new MenuItem("Fore Color")); color.add(item4=new MenuItem("Back Color")); format.add(color); mbar.add(format); mbar.add(file); mbar.add(format); mymenu mymenu1=new mymenu(this); MyWindowAdapter adapter=new MyWindowAdapter(this); addWindowListener(adapter); item1.addActionListener(mymenu1); item2.addActionListener(mymenu1); } public void paint(Graphics g) { g.drawString(msg,100,100); }

66

class MyWindowAdapter extends WindowAdapter { sampleframe frame1; public MyWindowAdapter(sampleframe frame2) { this.frame1=frame2; } public void windowClosing(WindowEvent we) { frame1.setVisible(false); } } class mymenu implements ActionListener { sampleframe frame1; public mymenu(sampleframe frame2) { this.frame1=frame2; } public void actionPerformed(ActionEvent ae) { String msg="U have selected "; String s=(String) ae.getActionCommand(); if(s.equals("New")) { msg+="New"; } if(s.equals("Open")) { msg+="Open "; } frame1.msg=msg; frame1.repaint(); }

67
public class menu1 extends Applet { Frame f; public void init() { f=new sampleframe("New frame created"); f.setSize(250,250); f.setVisible(true); } public void start() { f.setVisible(true); } public void stop() { //f.setVisible(false); } public void paint(Graphics g) { g.drawString("This is in applet window",10,20); } } Program Name : PANEL1~1 JAV import java.awt.*; import java.awt.event.*; import javax.swing.*; (panel1.java)

public class panel1 extends JFrame implements ActionListener { JPanel mypanel; Container c; Button b[]; public panel1() { super("PanelLayout Demo"); mypanel=new JPanel(); c=getContentPane(); b=new Button[5]; mypanel.setLayout(new GridLayout(1,b.length)); for(int i=0;i<b.length;i++) { b[i]=new Button("Button"+(i+1)); b[i].addActionListener(this); mypanel.add(b[i]); } c.add(mypanel,BorderLayout.SOUTH); setSize(425,150); show(); } public void actionPerformed(ActionEvent ae) { }

68
public static void main(String args[]) { panel1 lay2=new panel1(); lay2.addWindowListener( new WindowAdapter() { public void WindowClosing(WindowEvent e) { System.exit(0); } } ); } }

Topic : Interface Topic : Package


Program Name : ACCOUN~1 JAV package Mypack; (AccountBalance.java)

---02

class Balance { String name; double bal; Balance(String n,double b) { name=n; bal=b; } void show() { if(bal<0) System.out.print("----->"); System.out.println(name+" : $"+bal); } } class AccountBalance { public static void main(String args[]) { Balance current[]=new Balance[3]; current[0]=new Balance("K.J.Fielding",123.23); current[1]=new Balance("Will Tell",157.02); current[2]=new Balance("Tom Jackson",-12.23); for(int i=0;i<3;i++) current[i].show(); } } Program Name : MYBALA~1 JAV import MyPack.*; (mybalance.java)

public class mybalance { public static void main(String args[]) { balance test=new balance("aaaa",888.90); test.show(); }

69

You might also like