Thursday 16 August 2012

Animal.java

//U need to copy these images to a folder where you are going to save this program!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.applet.*;
/*<applet code="Animal" width=1000 height=700>
</applet>*/
public class Animal extends JApplet implements ActionListener,ListSelectionListener
{
JList li1;
JLabel l1;
JScrollPane s1;
JButton b1;
String [] Animals={"Dog","Elephant","Lion","Rat","Tiger"};
ImageIcon i1=new ImageIcon("Dog.png");
ImageIcon i2=new ImageIcon("Elephant.png");
ImageIcon i3=new ImageIcon("Lion.png");
ImageIcon i4=new ImageIcon("Rat.png");
ImageIcon i5=new ImageIcon("Tiger.png");
public void init()
{
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
li1=new JList(Animals);
li1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
s1=new JScrollPane(li1);
l1=new JLabel();
b1=new JButton("Clear");
cp.add(s1);
cp.add(l1);
cp.add(b1);
li1.addListSelectionListener(this);
b1.addActionListener(this);
}
public void valueChanged(ListSelectionEvent le)
{
int i;
i=li1.getSelectedIndex();
if(i==0)
{
l1.setIcon(i1);
}
else if(i==1)
{
l1.setIcon(i2);
}
else if(i==2)
{
l1.setIcon(i3);
}
else if(i==3)
{
l1.setIcon(i4);
}
else if(i==4)
{
l1.setIcon(i5);
}
}
public void actionPerformed(ActionEvent ae)
{
l1.setVisible(false);
li1.clearSelection();
}
}

1 comment: