//WAP to check the combobox
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class combo extends JFrame implements ItemListener
{
JComboBox cb;
Container c;
combo()
{
c=getContentPane();
c.setLayout(null);
cb=new JComboBox();
cb.addItem("Red");
cb.addItem("Blue");
cb.addItem("Pink");
cb.setBounds(50,50,100,30);
cb.addItemListener(this);
c.add(cb);
}
public void itemStateChanged(ItemEvent ie)
{
String str=(String)cb.getSelectedItem();
if(str=="Red")
{
c.setBackground(Color.red);
}
if(str=="Blue")
{
c.setBackground(Color.blue);
}
if(str=="Pink")
{
c.setBackground(Color.pink);
}
}
public static void main(String args[])
{
combo f=new combo();
f.setTitle("demo");
f.setSize(500,500);
f.setVisible(true);
}
}
No comments:
Post a Comment