Core Java - [Applet: AWT Checkbox Class]

♠ Posted by Unknown in at 05:53

Applet: AWT Checkbox Class

//Write a Java Applet that create a some checkboxes and three radio buttons.

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

public class CheckBoxApplet extends Applet implements ItemListener
{
    
    String msg = "";
    Checkbox chkFirst, chkSecond, chkThird, chkMale, chkFemale;
    CheckboxGroup cbg;

    public void init()
    {
            chkFirst = new Checkbox("Reading");
            chkSecond = new Checkbox("Writing");
            chkThird = new Checkbox("Dancing");
Image: Java Checkbox Example
            cbg = new CheckboxGroup();
            chkMale = new Checkbox("Male", cbg, true);
            chkFemale = new Checkbox("Female", cbg, false);
            
            add(chkFirst);
            add(chkSecond);
            add(chkThird);
            add(chkMale);
            add(chkFemale);

            chkFirst.addItemListener(this);
            chkSecond.addItemListener(this);
            chkThird.addItemListener(this);
            chkMale.addItemListener(this);
            chkFemale.addItemListener(this);
    }

    public void itemStateChanged(ItemEvent ie)
    {
            repaint();
    }
    public void paint(Graphics g)
    {
            msg = "Current State : "; 
            g.drawString(msg, 6, 80);
            msg = "Reading : " + chkFirst.getState();
            g.drawString(msg, 6, 100);
            msg = "Writing : " + chkSecond.getState();
            g.drawString(msg, 6, 120);
            msg = "Dancing : " + chkThird.getState();
            g.drawString(msg, 6, 140);
            msg = "Current Selection :";
            msg += cbg.getSelectedCheckbox().getLabel();
            g.drawString(msg, 6, 200);
    }
}

0 comments:

Post a Comment