//组合框的选项是对象,不一定是字符串,比如可以显示图象,但最好不要将字符串和图象混合了放在一个组合框里
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.UIManager;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
public class JComboBoxDemo
{
public static void main(String[] args)
{
try {UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName ());}catch(Exception e){}
String[] names = {"孙中山", "袁世凯", "毛泽东", "蒋介石", "邓小平", "天安
门"};
MyBox equals = new MyBox (names);
equals.addItem ("游神");
equals.addItem ("猪头");
MyBox images = new MyBox ();
images.addItem (new ImageIcon ("Image//QQ.png"));
images.addItem (new ImageIcon ("Image//fairy.gif"));
JPanel panel = new JPanel(new FlowLayout (FlowLayout.CENTER, 0, 1));
panel.setPreferredSize (new Dimension (300,100));
panel.add (equals);
panel.add (images);
JFrame frame = new JFrame("人名");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setContentPane (panel);
frame.pack();
frame.show();
}
}
class MyBox extends JComboBox
{
public MyBox(){}
public MyBox(Object[] boj)
{
super(boj);
setEditable (true);
setAlignmentX (Component.LEFT_ALIGNMENT);
}
}
评论