Tuesday, 9 December 2014

ItemListenerDemo in Java


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

public class ItemListenerDemo extends Applet implements ItemListener
{
     CheckboxGroup radioGroup;
     Checkbox radio1;
     Checkbox radio2;
     Checkbox radio3;

    public void init()
    {
          radioGroup = new CheckboxGroup();
          radio1 = new Checkbox("Red",radioGroup,false);
          radio2 = new Checkbox("Blue",radioGroup,false);
          radio3 = new Checkbox("Green",radioGroup,false);
         
          add(radio1);
          radio1.addItemListener(this);
          add(radio2);
          radio2.addItemListener(this);
          add(radio3);
          radio3.addItemListener(this);
     }

    public void paint(Graphics g)
    {
        int x = getSize().width;
        int y = getSize().height;

        x = x/2;
        y = y/2;

        if(radioGroup.getSelectedCheckbox() == radio1)
        {
            g.setColor(Color.red);
        }
        else if(radioGroup.getSelectedCheckbox() == radio2)
        {
            g.setColor(Color.blue);
        }
        else
        {
            g.setColor(Color.green);
        }

        g.drawString("Welcome to Java...",x,y);
    }

    public void itemStateChanged(ItemEvent ie)
    {
        repaint();
    }
}

/*


*/
Output

No comments:

Post a Comment