正文

JComboBox用法2005-09-17 00:57:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/hurongliang/4844.html

分享到:

//组合框的选项是对象,不一定是字符串,比如可以显示图象,但最好不要将字符串和图象混合了放在一个组合框里

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);
 }
}
 

阅读(8941) | 评论(1)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册