Monday 23 June 2014

Program to show the example of choice

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class  choic extends Applet implements ItemListener
{
Choice ch1;
public void init()
{
ch1=new Choice();
ch1.add("green");
ch1.add("red");
ch1.add("blue");
add(ch1);
ch1.addItemListener(this);
}
public void itemStateChanged(ItemEvent e) {
if(e.getSource()==ch1)
{
int c =ch1.getSelectedIndex();
switch(c)
{
case 0:
this.setBackground(Color.green);
break;
case 1:
this.setBackground(Color.red);
break;
case 2:
this.setBackground(Color.blue);
break;
default:
this.setBackground(Color.black);


}

}}}

output:


No comments:

Post a Comment

SAY HELLO!!