JTextArea 只能包含数字,但允许负值

发布于 2024-12-07 11:22:03 字数 1594 浏览 3 评论 0原文

我有一个 JTextArea 只需要接受数字。这是我的代码:

DocumentFilter onlyNumberFilter = new AxisJTextFilter();
    final JTextArea areaTextoXMin = new JTextArea(String.valueOf(xMin));
    ((AbstractDocument)areaTextoXMin.getDocument()).setDocumentFilter(onlyNumberFilter);

对于正数可以正常工作,但对于负数则不行。我该如何解决这个问题?

编辑:抱歉,AxisJTextFilter 是通过互联网找到的,我忘记了。其代码为:

import javax.swing.text.*;
import java.util.regex.*;

public class AxisJTextFilter extends DocumentFilter
{
    public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException
    {
        StringBuilder sb = new StringBuilder();
        sb.append(fb.getDocument().getText(0, fb.getDocument().getLength()));
        sb.insert(offset, text);
        if(!containsOnlyNumbers(sb.toString())) return;
        fb.insertString(offset, text, attr);
    }
    public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attr) throws BadLocationException
    {
        StringBuilder sb = new StringBuilder();
        sb.append(fb.getDocument().getText(0, fb.getDocument().getLength()));
        sb.replace(offset, offset + length, text);
        if(!containsOnlyNumbers(sb.toString())) return;
        fb.replace(offset, length, text, attr);
    }
    public boolean containsOnlyNumbers(String text)
    {
        Pattern pattern = Pattern.compile("\\d*(\\.\\d{0,3})?");
        Matcher matcher = pattern.matcher(text);
        boolean isMatch = matcher.matches();
        return isMatch;
    }
}

I have a JTextArea which only has to accept numbers. This is my code:

DocumentFilter onlyNumberFilter = new AxisJTextFilter();
    final JTextArea areaTextoXMin = new JTextArea(String.valueOf(xMin));
    ((AbstractDocument)areaTextoXMin.getDocument()).setDocumentFilter(onlyNumberFilter);

Works OK for positive numbers, but not for negative numbers. How can I fix that?

EDIT: Sorry, the AxisJTextFilter was found over the Internet and I forgot about that. Its code is:

import javax.swing.text.*;
import java.util.regex.*;

public class AxisJTextFilter extends DocumentFilter
{
    public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException
    {
        StringBuilder sb = new StringBuilder();
        sb.append(fb.getDocument().getText(0, fb.getDocument().getLength()));
        sb.insert(offset, text);
        if(!containsOnlyNumbers(sb.toString())) return;
        fb.insertString(offset, text, attr);
    }
    public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attr) throws BadLocationException
    {
        StringBuilder sb = new StringBuilder();
        sb.append(fb.getDocument().getText(0, fb.getDocument().getLength()));
        sb.replace(offset, offset + length, text);
        if(!containsOnlyNumbers(sb.toString())) return;
        fb.replace(offset, length, text, attr);
    }
    public boolean containsOnlyNumbers(String text)
    {
        Pattern pattern = Pattern.compile("\\d*(\\.\\d{0,3})?");
        Matcher matcher = pattern.matcher(text);
        boolean isMatch = matcher.matches();
        return isMatch;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不弃不离 2024-12-14 11:22:03

尝试修改正则表达式(验证方法 containsOnlyNumbers)。

Pattern pattern = Pattern.compile("^[\\-\\+]?\\d+(\\.\\d+)?$");

这将接受以下号码:

  • 1234
  • -1234
  • +1234
  • 1234.1234

我希望它对

Udi有帮助

Try modifying the regular expression (int the validation method containsOnlyNumbers).

Pattern pattern = Pattern.compile("^[\\-\\+]?\\d+(\\.\\d+)?$");

This will accept the following numbers:

  • 1234
  • -1234
  • +1234
  • 1234.1234

I hope it helps

Udi

仙女 2024-12-14 11:22:03

这是对 Udi 优秀帖子的评论,但我不知道该怎么做。

我认为他的模式(d+)至少需要1位数字,应该是d?允许 0-N 位数字。因为,当用户输入时,“-”是合法的输入。小数点后面不带数字始终是合法的,尤其是在打字时。只有在最后(当你失去焦点等,YMMV)时,你才可以要求至少一位数字,或者,通融和务实,将字符串“-”视为 0。

并且在开发这些常规类型时表达式不要忘记用户可能正在复制和粘贴。

This is intended as a comment on Udi's excellent post, but I can't figure out how to do that.

I think his pattern (d+) requires at least 1 digit, and it should be d? to allow 0-N digits. Because, as the user is typing, "-" is legal input. And a decimal point followed by no digits is always legal, especially while typing. It's only at the end (when you lose focus, etc., YMMV) that you can either require at least one digit, or, be accommodating and pragmatic and just treat the string "-" as 0.

And when developing these kinds of regular expressions don't forget that the user might be copying and pasting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文