Vous êtes sur la page 1sur 49

//1.

SUBSTRING REMOVAL
import java.lang.*;
import java.io.*;

class Removestring
{
public static void main (String args[]) throws IOException
{
String s,rs,s1,s2;
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n\t\tSUBSTRING REMOVAL");
System.out.println("\t\t********* *******");
System.out.print("\nEnter the String : ");
s = new String(in.readLine());
System.out.print("Enter the SubString : ");
String ss = new String(in.readLine());
int i =s.indexOf(ss);
int len =ss.length();
int k=i+len;
s1=s.substring(0,i);
s2=s.substring(k);
rs=s1+s2;
System.out.println("Resultant String is : " + rs);
}
catch (Exception e)
{
System.out.println("SUBSTRING NOT FOUND");
}
}
}

1
/*
Compile: javac Removestring .java
Run: java Removestring
*/

OUTPUT:

SUBSTRING REMOVAL
********* *******

Enter the String : I dont like music


Enter the SubString : dont
Resultant String is : I like music

E:\java\bin\java>java Removestring

SUBSTRING REMOVAL
********* *******

Enter the String : I dont like music


Enter the SubString : java
SUBSTRING NOT FOUND

2
// 2.AREA AND PERIMETER OF TRIANGLE
import java.io.*;
class Triangle
{
int width,height,length;
Triangle(int width1, int height1, int length1)
{
width=width1;
height=height1;
length=length1;
}
void area()
{
System.out.println("Area of the Triangle:" +(length*width)/2);
}
void perimeter()
{
System.out.println("Perimeter of the Triangle:" +(length+width+height));
}
public static void main(String args[])
{
try
{
int w,h,l;
BufferedReader ob=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Width:");
w=Integer.parseInt(ob.readLine());
System.out.print("Enter Height:");
h=Integer.parseInt(ob.readLine());
System.out.print("Enter Length:");
l=Integer.parseInt(ob.readLine());
Triangle tri=new Triangle(w,h,l);
tri.area();
tri.perimeter();
}
catch(Exception e)
{
System.out.println(e);
}
}

3
/*

Compile: javac Triangle.java


Run: java Triangle
*/

OUTPUT:

Enter Width : 10

Enter Height : 10

Enter Length : 10

Area of the Triangle : 50

Perimeter of the Triangle : 30

4
//3.ORDERING RANDOM NUMBERS

import java.util.Arrays;
import java.util.Random;
public class randomnos
{
public static void main(String arg[])
{
int i, j,temp;
Random no=new Random();
int a[]=new int[10];
System.out.println("RANDOM NUMBERS");
System.out.println("******** **********");

for(i=0;i<10;i++)
{
a[i]=no.nextInt(100);
System.out.println(a[i]+" ");
}
Arrays.sort(a);
System.out.println("Ascending order arrangement of random numbers");
for(i=0;i<10;i++)
System.out.println(a[i]+" ");
}
}

5
OUTPUT:

RANDOM NUMBERS
********* **********
43
43
59
78
47
77
51
68
53
10

Ascending order arrangement of random numbers


10
43
43
47
51
53
59
68
77
78

6
//4.DISPLAYING DATE USING CALENDAR CLASS

import java.util.*;
import java.util.Calendar;
class calen
{
public static void main(String arg[])
{
String months[]={"jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};
Calendar cal=Calendar.getInstance();

System.out.println("DATE:");
System.out.print(months[cal.get(Calendar.MONTH)]);
System.out.print(""+cal.get(Calendar.DATE)+"");

System.out.println("TIME:");
System.out.print(cal.get(Calendar.HOUR)+":");
System.out.print(cal.get(Calendar.MINUTE)+":");
System.out.print(cal.get(Calendar.SECOND)+"");

cal.set(Calendar.HOUR,10);
cal.set(Calendar.MINUTE,29);
cal.set(Calendar.SECOND,22);

System.out.println("Updated time:");

System.out.print(cal.get(Calendar.HOUR)+":");
System.out.print(cal.get(Calendar.MINUTE)+":");
System.out.print(cal.get(Calendar.SECOND)+"");
}
}

7
OUTPUT:

DATE:
Feb 28

TIME:
9:52:55

Updated time:
10:29:22

8
// 5. IMAGE MANIPULATION

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

public class imag extends Applet


{
public void paint(Graphics g)
{

g.drawOval(40,40,120,150);
g.drawOval(57,75,30,20);
g.drawOval(110,75,30,20);
g.drawOval(85,100,30,20);
g.drawOval(160,92,15,30);
g.drawOval(25,92,15,30);
g.fillOval(68,81,10,10);
g.fillOval(121,81,10,10);
g.fillArc(60,125,80,40,180,180);
}
}
/* <applet code=imag height = 400 width=400>
</applet>*/

/*Compile: javac imag.java


Run: appletviewer imag.java
*/

9
OUTPUT:

10
//6. STRING MANIPULATION

import java.lang.*;
import java.io.*;
class manip
{
public static void main(String arg[])
{

char ss1[] = {'W','E','L','C','O','M','E'};


char ss2[]={'G','O','O','D','M','O','R','N','I','N','G'};
String s1=new String(ss1);
String s2=new String(ss2);
System.out.println("\n\n\t STRING MANIPULATION");
System.out.println("\t ****** ************");
System.out.println("\n First String : " + s1 + " \tSecond String: " + s2);
String s3=s1.concat(s2);
System.out.println("\n\nThe Concatenated String is : " + s3);
System.out.println();
String s4=s3.substring(3,7);
System.out.println("The Substring is : "+ s4);
System.out.println();
if(s1.equals(s2))
{
System.out.println("The Strings are equal.");
System.out.println();
}
else
{System.out.println("The Strings are not equal.");
System.out.println();
}
String s5="India".replace('i','y');
System.out.println("After replacing India i with y is : " + s5);
System.out.println();
String s6="FLOWER";
System.out.println("Conversion of Lowercase : "+s6.toLowerCase());
System.out.println();
System.out.println("Index of n in India = "+s5.indexOf('n'));
}
}

11
OUTPUT:

STRING MANIPULATION
******* *****************

First String : WELCOME Second String: GOODMORNING

The Concatenated String is : WELCOMEGOODMORNING

The Substring is : COME

The Strings are not equal.

After replacing India i with y is : Indya

Conversion of Lowercase : flower

Index of n in India = 1

12
//7.USAGE OF VECTOR CLASS

import java.lang.*;
import java.io.*;
import java.util.*;

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

Vector list=new Vector();


int length=args.length;
for(int i=0;i<length;i++)
{
list.addElement(args[i]);
}
list.insertElementAt(COBOL,2);
int size=list.size();
String listArray[]=new String[size];
list.copyInto(listArray);
System.out.println(List of Languages are );
for(int i=0;i<size;i++)
{
System.out.println(listArray[i]);
}
}
}

13
/* compile : javac SampleVector.java

Run : java SampleVector C C++ VB JAVA


*/

OUTPUT:

List of Languages are

C
C++
COBOL
VB
JAVA

14
//8.USAGE OF PACKAGE AND INTERFACE

package Mypackage;

public class MathFun


{

public int square(int num)


{
return(num * num);
}

public int cube(int num)

{
return(num * num * num);
}

public static void main(String args[])


{
System.out.println("Package Created");
}

/* compile : javac MathFun.java


Run : java Mypackage.MathFun */

15
import Mypackage.MathFun;

import java.io.*;
interface Area
{
final static float pi=3.14F;
float compute(float X,float Y);
}
class Rectangle implements Area
{
public float compute(float X,float Y)
{
return(X*Y);
}
}
class Circle implements Area
{
public float compute(float X,float Y)
{
return(pi*X*Y);
}
}
class InterfaceTest
{
public static void main(String arg[])
{
MathFun mf = new MathFun();
System.out.println(Square = + mf(5));
System.out.println(Cube = + mf(3));
Rectangle rect=new Rectangle();
Circle cir=new Circle();
System.out.print("Area of the rectangle: "+rect.compute(10,20));
System.out.print("Area of the circle: "+cir.compute(10,0));
}
}

16
/* compile : javac InterfaceTest.java
Run : java InterfaceTest */

OUTPUT:

Square = 25
Cube = 27
Area of the rectangle: 200.0
Area of the circle: 0.0

17
// 9. THREAD BASED APPLICATION AND EXCEPTION HANDLING
import java.lang.*;
class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
if(i==1)
resume();
System.out.println("From Thread A:i=" +i);
}

System.out.println("Exit from Thread A");


}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{

System.out.println("from Thread B:j=" +j);


if(j==6)
try
{
wait();
}
catch(InterruptedException e){}
}

System.out.println("Exit from Thread B");


}
}
class C extends Thread
{
public void run()
{
for(int k=1;k<=5;k++)
{

System.out.println("FromThread C:k=" +k);


if(k==1)
try

18
{
sleep(2000);
}
catch(InterruptedException e){}
}
System.out.println("Exit from Thread C");
}
}
class ThreadMethod
{
public static void main(String args[])
{
A threadA=new A();
B threadB=new B();
C threadC=new C();
System.out.println("Start thread A");
threadA.start();
System.out.println("Start thread B");
threadB.start();
System.out.println("Start thread C");
threadC.start();
System.out.println("End of main thread");
}
}

/*
COMPILE: javac ThreadMethod.java
RUN: java ThreadMethod
*/

19
OUTPUT:

Start thread A
Start thread B
Start thread C
End of main thread
FromThread C: k=1
From Thread B: j=1
FromThread A: i=1
From Thread B: j=2
FromThread A: i=2
From Thread B: j=3
FromThread A: i=3
From Thread A: i=4
FromThread A: i=5
Exit from Thread A
From Thread B: j=4
FromThread B: j=5
Exit from Thread B
From Thread C: k=2
From Thread C: k=3
From Thread C: k=4
From Thread C: k=5
Exit from Thread C

20
//10. SYNCHRONIZED METHOD

import java.io.*;
import java.lang.Thread;
import java.lang.System;
class SyncThread extends Thread{
static String message[]={"I","LOVE","MY","MOM","VERY"," MUCH"};
public SyncThread(String id)
{
super(id);
}
public void run()
{
Sync.displayList(getName(),message);
}
void waiting()
{
try
{
sleep(3000);
}
catch(InterruptedException e)
{
System.out.println("Interrupted");
}
}
}
class Sync
{
public static synchronized void displayList(String name,String list[])
{
for(int i=0;i<list.length;i++)
{
SyncThread thread=(SyncThread)Thread.currentThread();
thread.waiting();
System.out.println(name +":"+ list[i]);
}
}
}
class SynchronizedThread
{
public static void main(String args[])
{
SyncThread thread1=new SyncThread("Thread1");
SyncThread thread2=new SyncThread("Thread2");

21
thread1.start();
thread2.start();
}
}

/*
COMPILE: javac SynchronizedThread.java
RUN: java SynchronizedThread
*/

OUTPUT:

Thread1:I
Thread1:LOVE
Thread1:MY
Thread1:MOM
Thread1:VERY
Thread1:MUCH
Thread2:I
Thread2:LOVE
Thread2:MY
Thread2:MOM
Thread2:VERY
Thread2:MUCH

22
// 11.COUNTING NO. OF CHARACTERS, WORDS & LINES
import java.io.*;
class FileRead
{
long word(String line)
{
int index =0;
long numwords = 0;
boolean prevwhitespace = true;
while(index < line.length())
{
char c = line.charAt(index++);
boolean currwhitespace = Character.isWhitespace(c);
if(prevwhitespace && !currwhitespace)
{
numwords++;
}
prevwhitespace = currwhitespace;
}
return numwords;
}

public static void main(String args[])


{
int l=0;
long w=0,temp=0,cc=0;
String strLine;
char c;
try{
FileInputStream fstream = new FileInputStream("inputtext.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
FileRead o = new FileRead();
strLine = br.readLine();
while(strLine !=null)
{
temp = o.word(strLine);
w += temp;
cc += strLine.length();
l++;
strLine = br.readLine();
}
System.out.println("Number of characters : " + cc);
System.out.println("Number of words : " + w);
System.out.println("Number of Lines : " + l);

23
in.close();
}
catch(Exception e){
System.err.println("Error :" + e.getMessage());
}
}
}

OUTPUT:

Number of characters : 61
Number of words : 11
Number of Lines : 2

24
// 12. ELECTRICITY BILL

import java.io.*;
class Elect
{
public static void main(String arg[])
{
try
{
FileWriter fStream=new FileWriter("EBOutput.txt",true);
BufferedWriter out=new BufferedWriter(fStream);
String n;
double eb,pr,cr;
double c,rate=0.0;
double usage=0.0;
BufferedReader ob=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the name:");
n=ob.readLine();
System.out.println("Enter the EB No:");
eb=Double.parseDouble(ob.readLine());
System.out.println("Enter the previous month reading:");
pr=Double.parseDouble(ob.readLine());
System.out.println("Enter the current month reading:");
cr=Double.parseDouble(ob.readLine());
c=cr-pr;
if(c<=100)
rate=1.5;
else if(c>100 && c<=200)
rate=2.00;
else if(c>200 && c<=250)
rate=2.50;
else if(c>250)
rate=4.00;

usage=c*rate;
System.out.println("Customer will be charged:" + usage);
out.write("Customer Name:" + n);
out.write("EB.no" + eb);
out.write("Previous reading"+pr);
out.write("Current Reading"+cr);
out.close();
}catch(Exception e){
System.err.println("Error"+e.getMessage());
}
}

25
}

OUTPUT:

Enter the name:


Saravanan
Enter the EB No. :
1548
Enter the previous month reading:
8765
Enter the current month reading:
8910
Customer will be charged : 290.0

26
//13.TELEPHONE BILLING

import java.io.*;
class Phone
{
public static void main(String arg[])
{
try
{
FileWriter fStream=new FileWriter("out.txt",true);
BufferedWriter out=new BufferedWriter(fStream);
String N;
double calls,ccost,ph;
double rate=0.0;
BufferedReader ob=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Customer Name:");
N=ob.readLine();
System.out.println("Enter the phone.no:");
ph=Double.parseDouble(ob.readLine());
System.out.println("Enter the Tarrif value:");
ccost=Double.parseDouble(ob.readLine());
System.out.println("Enter the no.of calls:");
calls=Double.parseDouble(ob.readLine());
rate = calls*ccost+200;
System.out.println("Customers will be charged:" + rate);

out.write("Name:"+N);
out.write("eb.no"+ph);
out.write("Tarrif value"+ccost);
out.write("no.of calls"+calls);
out.close();
}catch(Exception e){
System.err.println("Error"+e.getMessage());
}
}
}

27
OUTPUT:

Enter the Customer Name:


Suseela
Enter the phone no. :
24642264
Enter the Tariff value:
50.0
Enter the no. of calls:
124
Customer will be charges : 6400.0

28
// 17. WORKING WITH FRAMES AND CONTROLS

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

class FramCtrl extends Frame implements ItemListener


{
Checkbox ch1,ch2,ch3;
CheckboxGroup chg;
List l;
FramCtrl()
{
setSize(1000,1000);
setLayout(new FlowLayout());
chg=new CheckboxGroup();
ch1=new Checkbox("COUNTRY",chg,false);
ch2=new Checkbox("STATE",chg,false);
ch3=new Checkbox("CITY",chg,false);
add(ch1);
add(ch2);
add(ch3);
l=new List();
add(l);
ch1.addItemListener(this);
ch2.addItemListener(this);
ch3.addItemListener(this);
setVisible(true);
}

public void itemStateChanged(ItemEvent ie)


{
if(ch1.getState())
{
l.removeAll();
l.add("INDIA");
l.add("SWITZERLAND");
l.add("AMERICA");
}
else if(ch2.getState())
{
l.removeAll();
l.add("ANDHRA PRADESH");
l.add("TAMILNADU");
l.add("KERALA");
}

29
else if(ch3.getState())
{
l.removeAll();
l.add("AVADI");
l.add("ANNA NAGAR");
l.add("ADYAR");
}

}
public static void main(String args[])
{
FramCtrl ob=new FramCtrl();
}
}

30
OUTPUT:

31
// 20.WORKING WITH DIALOG BOX AND MENUS

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
//<applet code=MenuDemo height=4000 width=4000></applet>

class menudialog extends Frame implements ActionListener{


Frame f=new Frame("MenuDialogBox");
Button b1,b2;

menudialog(String t)
{
super(t);
MenuBar mbar=new MenuBar();
Menu file=new Menu("File");
MenuItem it1,it2;
file.add(it1=new MenuItem("Open"));
file.add(it2=new MenuItem("Close"));
mbar.add(file);
setMenuBar(mbar);
it1.addActionListener(this);
it2.addActionListener(this);
}

public void actionPerformed(ActionEvent e)


{
Dialog fd=new Dialog(f,"Dialog Box");
String arg=(String)e.getActionCommand();
if(arg.equals("Open"))
{
fd.add(new Button("Open Pressed"));
fd.setSize(200,100);
fd.setVisible(true);
}
if (arg.equals("Close"))
{
fd.add(new Button("Close Pressed"));
fd.setSize(200,100);
fd.setVisible(true);
}
}
}

public class MenuDemo extends Applet

32
{
Frame f;
public void init()
{
f=new menudialog("MenuDialog");
f.setSize(200,100);
f.setVisible(true);
}
public void start()
{
f.setVisible(true);
}
}

33
OUTPUT:

34
//21. IMPLEMENTATION OF COLORS AND FONT CLASS

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

public class ColorApplet extends Applet


{
public void paint(Graphics g)
{
Font f=new Font("TimesRoman",Font.ITALIC,20);
Font f1=new Font("Courier",Font.PLAIN,22);
Font f2=new Font("Monotype Corsiva",Font.BOLD,24);
g.setColor(Color.green);
g.setFont(f);
g.drawString("Be Happy.Be Hopeful",50,70);
g.setColor(Color.blue);
g.setFont(f1);
g.drawString("ALL THE BEST",50,100);
g.setColor(Color.red);
g.setFont(f2);
g.drawString("God is Great",50,150);
}
}

//<applet code=ColorApplet height=400 width=400></applet>

35
OUTPUT:

36
//18. SHAPES USING GRAPHICAL STATEMENTS

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

public class Shapes extends Applet


{
public void paint(Graphics g)
{
g.setColor(Color.orange);
g.fillRect(250,80,200,50);
g.setColor(Color.green);
g.fillRect(250,180,200,50);
g.setColor(Color.blue);
g.drawOval(320,130,50,50);
}
}

/*<applet code = "Shapes" width = 500 height=500></applet>*/

37
OUTPUT:

38
//19. WORKING WITH PANELS AND LAYOUT

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

//<applet code=CardDemo width=500 height=500></applet>

public class CardDemo extends Applet implements MouseListener, ActionListener


{
Checkbox dos,lan,nt,win95;
Button b1,b2;
Panel MainPan,Dospan,Winpan;
CardLayout c;
public void init()
{
b1=new Button("Dos Based");
b2=new Button("Window Based");
add(b1);
add(b2);
c=new CardLayout();
MainPan = new Panel();
MainPan.setLayout(c);
dos=new Checkbox("Ms-Dos");
lan=new Checkbox("Novell Netware");
Dospan = new Panel();
Dospan.add(dos);
Dospan.add(lan);

nt = new Checkbox("Nt Server");


win95 = new Checkbox("Windows 95");
Winpan = new Panel();
Winpan.add(nt);
Winpan.add(win95);
MainPan.add(Dospan,"Dos Based");
MainPan.add(Winpan,"Window Based");
add(MainPan);

b1.addActionListener(this);
b2.addActionListener(this);
addMouseListener(this);
}

public void mousePressed(MouseEvent me)

39
{
c.next(MainPan);
}

public void mouseReleased(MouseEvent me){}


public void mouseClicked(MouseEvent me){}
public void mouseEntered(MouseEvent me){}
public void mouseExited(MouseEvent me){}

public void actionPerformed(ActionEvent ae)


{
if(ae.getSource() == b1)
{
c.next(MainPan);
c.show(Dospan,"Dos Based");
}
else
{
c.next(MainPan);
c.show(Winpan,"Windows Based");
}
}
}

40
OUTPUT:

41
//14. SIMPLE CALCULATOR
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Simcal extends JApplet implements ActionListener
{
JTextField XInput,YInput;
JLabel answer;
public void init()
{
Container content=getContentPane();
XInput=new JTextField("0");
XInput.setBackground(Color.white);
YInput=new JTextField("0");
YInput.setBackground(Color.white);
JPanel XPanel=new JPanel();
XPanel.setLayout(new BorderLayout());
XPanel.add(new Label("X="),BorderLayout.WEST);
XPanel.add(XInput,BorderLayout.CENTER);
JPanel YPanel=new JPanel();
YPanel.setLayout(new BorderLayout());
YPanel.add(new Label("Y="),BorderLayout.WEST);
YPanel.add(YInput,BorderLayout.CENTER);
JPanel buttonPanel=new JPanel();
buttonPanel.setLayout(new GridLayout(1,4));
JButton plus=new JButton("+");
plus.addActionListener(this);
buttonPanel.add(plus);
JButton minus=new JButton("-");
minus.addActionListener(this);
buttonPanel.add(minus);
JButton times=new JButton("*");
times.addActionListener(this);
buttonPanel.add(times);
JButton divide=new JButton("/");
divide.addActionListener(this);
buttonPanel.add(divide);
answer=new JLabel("result",JLabel.CENTER);
content.setLayout(new GridLayout(4,1,2,2));
content.add(XPanel);
content.add(YPanel);
content.add(buttonPanel);
content.add(answer);
XInput.requestFocus();
}

42
public Insets getInsets()
{
return new Insets(2,2,2,2);
}
public void actionPerformed(ActionEvent evt)
{
double X,Y;
try
{
String XStr=XInput.getText();
X=Double.parseDouble(XStr);
}
catch(NumberFormatException e)
{
answer.setText("illegal data for x");
return;
}
try
{
String YStr=YInput.getText();
Y=Double.parseDouble(YStr);
}
catch(NumberFormatException e)
{
answer.setText("illegal data for y");
return;
}
String op=evt.getActionCommand();

if(op.equals("+"))
answer.setText("ADDITION:"+(X+Y));
else if(op.equals("-"))
answer.setText("SUBTRACTION:"+(X-Y));
else if(op.equals("*"))
answer.setText("MULTIPLICATION:"+(X*Y));
else if(op.equals("/"))
{
if(Y==0)
answer.setText("Cant divide by Zero!!!!!");
else
answer.setText("Division :"+(X/Y));
}
}
}
/*<applet code="Simcal" width=500 height=500></applet>*/

43
OUTPUT:

44
//15. USAGE OF BUTTONS AND LABELS

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/* <applet code = ButActDemo height=500 width=500></applet>*/
public class ButActDemo extends Applet implements ActionListener
{
Button b1,b2;
Label L1;

public void init()


{
setLayout(null);
b1=new Button("Push");
b2=new Button("Java");
L1=new Label();
add(L1);
add(b1);
add(b2);
L1.setBounds(100,100,350,20);
b1.setBounds(10,10,50,20);
b2.setBounds(80,10,50,20);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String s;
s=ae.getActionCommand();
if(s == "Push")

L1.setText("Hai Friends");

if(s == "Java")
L1.setText("It is an Internet based programming Language");
}
}

45
OUTPUT:

46
//16.USAGE OF RADIO BUTTONS, CHECKBOX AND CHOICE LIST

import java.applet.Applet;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowListener;

public class Radiocheck extends Applet implements ItemListener


{
Choice itemchoice;
String s[]={"10","12","15","18","20","24","28"};
Checkbox ft1,ft2,st1,st2;
Label lbl,lsize,lfont,lstyle;
CheckboxGroup FontGroup;

public void init()


{
setSize(600,200);
setLayout(new GridLayout(8,4));
lfont = new Label("Font",Label.LEFT);
lstyle = new Label("Style",Label.RIGHT);
lsize = new Label("Size",Label.LEFT);
lbl = new Label("Select Font,Style and Size");
lbl.setFont(new Font(" Arial Black ",Font.PLAIN,18));
lbl.setForeground(Color.blue);
lbl.setAlignment(Label.CENTER);

st1=new Checkbox("Bold");
st2=new Checkbox("Italic");

FontGroup = new CheckboxGroup();


ft1=new Checkbox(" Times New Roman ",FontGroup,true);
ft2 = new Checkbox(" Comic Sans MS ",FontGroup,false);
itemchoice = new Choice();
for(int i=0;i<s.length;i++)
itemchoice.add(s[i]);
itemchoice.addItemListener(this);

add(lbl);
add(lfont);
add(lsize);
add(lstyle);
add(ft1);
add(ft2);

47
add(st1);
add(st2);
add(itemchoice);
}

public void itemStateChanged(ItemEvent e)


{
String str,str1;
str1="";
if(ft1.getState())
str=ft1.getLabel();
else
str= ft2.getLabel();

if(st1.getState())
str1=st1.getLabel();
if(st2.getState())
str1=str1 + st2.getLabel();
lbl.setText("U had chosen Font : " + str + " Style : " + str1 + " Size : " +
itemchoice.getSelectedItem());
}
}

/*<applet code = Radiocheck width = 500 height = 500> </applet>*/

48
OUTPUT:

49

Vous aimerez peut-être aussi