Tuesday, 4 September 2012

java program to use awt check box in applet.


// Demonstrate check boxes.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="CheckboxDemo" width=250 height=200>
</applet>
*/
public class CheckboxDemo extends Applet implements ItemListener
{
String msg = "";
Checkbox obj1, obj2, obj3, obj4;
public void init()
{
obj1 = new Checkbox("A",  false);
obj2 = new Checkbox("B",  false);
obj3 = new Checkbox("C",  true);
obj4 = new Checkbox("D",  false);
add(obj1);
add(obj2);
add(obj3);
add(obj4);
obj1.addItemListener(this);
obj2.addItemListener(this);
obj3.addItemListener(this);
obj4.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
//repaint();
}
// Display current state of the check boxes.
public void paint(Graphics g)
{
msg = "Current state: ";
g.drawString(msg, 6, 80);
msg = " A: " + obj1.getState();
g.drawString(msg, 6, 100);
msg = " B: " + obj2.getState();
g.drawString(msg, 6, 120);
msg = " C: " + obj3.getState();
g.drawString(msg, 6, 140);
msg = " D: " + obj4.getState();
g.drawString(msg, 6, 160);
}
}

Output: