日期:2014-05-20 浏览次数:20782 次
import java.awt.FlowLayout; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.Vector; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JTextField; public class JComboBoxDemo extends JFrame { /** * */ private static final long serialVersionUID = 1L; private JTextField jtf = null; private JComboBox jcb = null; private Vector<String> vdata = new Vector<String>(); private Vector<String> v2 = null; public JComboBoxDemo() { this.setLayout(new FlowLayout()); jtf = new JTextField(10); jtf.addKeyListener(new MyAdapter()); this.add(jtf); vdata.add("1"); vdata.add("32e"); vdata.add("22"); vdata.add("123"); v2 = new Vector<String>(vdata); jcb = new JComboBox(vdata); jcb.setSelectedIndex(0); jcb.setEditable(true); this.add(jcb); this.setTitle("Demo"); this.setSize(600, 450); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new JComboBoxDemo(); } class MyAdapter implements KeyListener { @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub String value = JComboBoxDemo.this.jtf.getText().trim(); JComboBoxDemo.this.jcb.removeAllItems(); for (String item : JComboBoxDemo.this.v2) { if (item.startsWith(value)) { JComboBoxDemo.this.jcb.addItem(item); } } JComboBoxDemo.this.jcb.validate(); } @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } } }
------解决方案--------------------
学习一下....
------解决方案--------------------
楼主的泡泡堂头像不错,JComboBox是可以设置成编辑的,你就改一下吧,看行不,总不能自己一点也不想吧,这样可不好啊。