Monday 23 June 2014

Program to show the example of LIST and add items to it

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

import javax.swing.JOptionPane;
public class  liss extends Applet implements ItemListener, ActionListener
{
List ch1;
Button b1,b2,b3;
TextField t;
public void init()
{
ch1=new List(6);
ch1.add("green");
ch1.add("red");
ch1.add("blue");
add(ch1);
t=new TextField(" ");
add(t);
b1=new Button("add");
add(b1);
b1.addActionListener(this);
b2=new Button("remove");
add (b2);
b2.addActionListener(this);
ch1.addItemListener(this);
ch1.addActionListener(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);


}

}}

public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1)
{
ch1.add(t.getText());
}
else
{
ch1.remove(ch1.getSelectedIndex());
}
}}


OUTPUT:

No comments:

Post a Comment

SAY HELLO!!