//WAP to check combobox in panel
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class mypanel extends JPanel implements ItemListener
{
JComboBox cb;
int a=0;
mypanel()
{
setLayout(null);
cb=new JComboBox();
cb.addItem("Rect");
cb.addItem("RoundRect");
cb.addItem("Circle");
cb.setBounds(50,50,100,30);
cb.addItemListener(this);
add(cb);
}
public void itemStateChanged(ItemEvent ie)
{
String str=(String)cb.getSelectedItem();
if(str=="Rect")
a=1;
if(str=="RoundRect")
a=2;
if(str=="Circle")
a=3;
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(a==1)
{
g.drawRect(300,200,50,50);
}
if(a==2)
{
g.drawRoundRect(100,250,50,50,20,20);
}
if(a==3)
{
g.drawOval(120,100,50,10);
}
}
}
class myframe extends JFrame
{
Container c;
myframe()
{
c=getContentPane();
mypanel p=new mypanel();
c.add(p);
}
}
class cpanel
{
public static void main(String args[])
{
myframe f=new myframe();
f.setTitle("combobox panel");
f.setSize(500,500);
f.setVisible(true);
}
}
No comments:
Post a Comment