import
java.awt.*;
import
java.awt.event.*;
import
javax.swing.*;
public
class swing extends JFrame implements ActionListener,ItemListener
{JComboBox
combo=new JComboBox();
JButton btnExit=new
JButton("Exit");
Container cp=getContentPane();
Color c=cp.getBackground();
public swing()
{ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
combo.addItem("Reset to Default
Color");
combo.addItem("Set background
color to red");
combo.addItem("Set background
color to green");
combo.addItem("Set background
color to blue");
cp.add(combo);
cp.add(btnExit);
btnExit.setBounds(85,120,70,30);
combo.setBounds(20,10,200,30);
combo.addItemListener(this);
btnExit.addActionListener(this);
}
public
void itemStateChanged(ItemEvent e)
{ String
s=(String)e.getItem();
if(s.compareTo("Reset to
Default Color")==0)
cp.setBackground(c);
if(s.compareTo("Set background
color to red")==0)
cp.setBackground(Color.red);
if(s.compareTo("Set background
color to green")==0)
cp.setBackground(Color.green);
if(s.compareTo("Set background
color to blue")==0)
cp.setBackground(Color.blue);
}
public
void actionPerformed(ActionEvent a)
{ String
s=a.getActionCommand();
if(s.equals("Exit"))
System.exit(0);
}
public
static void main(String[]args)
{
swing cd=new swing();
cd.setSize(new Dimension(250,200));
cd.setTitle("A swing
application");
cd.setVisible(true);
}
}
Output
|