如何控制JCombobox输入时只能录入整数?大家有什么方法?能否告诉我一下?我知道JTextField控制输入的方法是setDocument(new Document()),而JCombobox呢?查了很多资料,都没有找到,高手请帮忙!非常感谢!!!

解决方案 »

  1.   

    http://www.java2s.com/Code/Java/Swing-JFC/ComboBox.htm
    这里有你需要的例子.
      

  2.   

    JCombobox在输入时,其Editor应该也是继承的JTextField。所以可以通过重写其Editor来实现控制输入。
      

  3.   

    zzhzzh204553(真的好想你):拜托,哪个例子是?请明示!没看到哪个是!
      

  4.   

    favorite7w():你说的好像不对,JComboBox的Editor是ComboBoxEditor,好像不是继承的JTextField
      

  5.   

    /**
    *你应该监听键盘事件!!!
    */
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JOptionPane;
    public class Textfield extends Applet implements KeyListener
    {
    TextField text1;
    public void init()
    {
    text1 = new TextField(20);
    add(new Label("输入数字: "));
    add(text1);
    text1.addKeyListener(this);
    }
    public void keyPressed(KeyEvent e)
    {
    if("0123456789".indexOf(e.getKeyChar()+"")==-1)
    {
    JOptionPane.showMessageDialog(this,"非法字符"," 警告对框",JOptionPane.WARNING_MESSAGE);
    text1.setText(null);return;
    }
    }
    public void keyReleased(KeyEvent e)
    {
    if("0123456789".indexOf(e.getKeyChar()+"")==-1)
    {
    JOptionPane.showMessageDialog(this,"非法字符"," 警告对框",JOptionPane.WARNING_MESSAGE);
    text1.setText(null);return;
    }
    }
    public void keyTyped(KeyEvent e)
    {}
    }