下拉列表(JComboBox)通常显示一个可选条目,允许用户在一个下拉列表中选择不同条目,用户也可以在文本区内输入选择项。
package first;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import com.sun.xml.internal.fastinfoset.algorithm.IEEE754FloatingPointEncodingAlgorithm;
class CobTest extends JFrame implements ItemListener
{
JLabel jlb;
ImageIcon france,germany,italy,japan;
public CobTest(String s) {
// TODO Auto-generated constructor stub
setSize(300,200);
setVisible(true);
setTitle(s);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
JComboBox jc=new JComboBox();
jc.addItem("中国");
jc.addItem("俄罗斯");
jc.addItem("韩国");
jc.addItem("联合国");
jc.addItemListener(this);
add(jc);
jlb=new JLabel(new ImageIcon("./image/中国.jpg"));
add(jlb);
validate();
}
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
String s=(String)e.getItem().toString();
jlb.setIcon(new ImageIcon("./image/"+s+".jpg"));
}
}
public class Test
{
public static void main(String[] args)
{
new CobTest("下拉列表框示例");
}
}