设置一下状态位不就行了吗?不过java的消息机制应该有更好的方法实现!

解决方案 »

  1.   

    有没有办法在JDialog关闭时把回车键吃掉?
      

  2.   

    用keyPressed可以吗?keyPressed不会有这个问题。
      

  3.   

    I use copy&paste to add some non-digits, can you detect?
    deal with keyReleased is not good.
      

  4.   

    my Suggestion:
    subclass JTextField, 
    add DocumentListener to detect if input is valid
      

  5.   

    检查的代码是什么意思?
    是说,检查输入的是不是数字么?
    如果要是那样的话,大可不必阿
    你可以在输入是就得知按键是什么,在那时判断
    我的意思就是,当输入的不是数字时,自动让JDialog弹出,不用Enter键
      

  6.   

    studying!!!!!           upupup!
      

  7.   

    判断是否为"0"-"9"和Enter键的值不就可以吗?
      

  8.   

    以前写的
    import javax.swing.text.*;
    import java.math.BigDecimal;public class NumberFormatDocument
        extends PlainDocument
    {
        private int textLength = -1;    private int scale = -1;    public NumberFormatDocument()
        {
            init();
        }    public NumberFormatDocument(int len)
        {
            this.textLength = len;
            init();
        }    public NumberFormatDocument(int len, int scale)
        {
            if(len > 0)
                this.textLength = len;
            if(scale > 0)
                this.scale = scale;
            init();
        }    public void setLength(int len)
        {
            if(len > 0)
                this.textLength = len;
            init();
        }    public void setScale(int scale)
        {
            if(scale > 0)
                this.scale = scale;
            init();
        }    protected void init()
        {
            try
            {
                if(0 < scale)
                {
                    String n = "0.";
                    for(int i = 0; i < scale; i++)
                    {
                        n += "0";
                    }
                    this.replace(0,this.getLength(),n,null);
                }
                else
                    this.replace(0,this.getLength(),"0",null);
            }
            catch(Exception e)
            {        }
        }    public void insertString(int offs, String str, AttributeSet a)
            throws BadLocationException
        {
            if (str == null || "".equals(str))
            {
                if (this.getLength() == 0)
                {
                    str = "0";
                    if (scale > 0)
                    {
                        str += ".";
                        for (int i = 0; i < scale; i++)
                        {
                            str += "0";
                        }
                    }
                    insertNumber(offs, str, a);
                }
                return;
            }
            insertNumber(offs, str, a);
        }    private void insertNumber(int offs, String str, AttributeSet a)
            throws BadLocationException
        {
            String text = "";
            int len = 0;
            char strFirstChar = str.charAt(0);
            if (0 == offs)
            {
                if (strFirstChar == '-' || strFirstChar == '+')
                {
                    text = str.substring(1);
                }
                else if (strFirstChar == '.' && scale > 0)
                {
                    str = "0" + str;
                    if (str.length() > 1)
                    {
                        text = str;
                    }
                }
                else
                {
                    text = str;
                }
                len = text.length();
            }
            else
            {
                text = this.getText(0, offs) + str +
                    this.getText(offs, getLength() - offs);
                if (this.getText(0, 1).equals("-") ||
                    this.getText(0, 1).equals("+"))
                {
                    len = text.length() - 1;
                }
                else
                {
                    len = text.length();
                }
            }
            int dot = text.indexOf('.');
            if(dot > 0)
            {
                if ( -1 != textLength && dot > textLength)
                    return;
                if(scale < 0 || (scale > 0 && len - dot - 1 > scale))
                {
                    return;
                }
            }
            else
            {
                if ( -1 != textLength && len > textLength)
                    return;
            }        if (!"".equals(text))
            {
                try
                {
                    new BigDecimal(text);
                }
                catch (Exception e)
                {
                    return;
                }
            }        super.insertString(offs, str, a);
        }
    }
      

  9.   

    问题解决了,用shadowcain()提供的方法,将keyrelease方法改成了keypress方法,谢谢大家!散分