JTextField限制只能输入特定字符

1. 背景

最近写了一个公司内部用的通用MQTT协议JMeter自定义采样器,自定义表达式的处理手法与《JMeter通用Http采样器》https://blog.csdn.net/camelials/article/details/127135630 一致。不同的是协议变了、荷载构造方式变了等。另外,由于结合了自身应用的业务,因此开发难度相比之前的JMeter通用Http采样器大了很多。由于对于请求对象的组装使用了效率相对不高的反射方式,同时还要结合表达式的计算,因此索性把这些事情放在了采样器的前置逻辑阶段用多线程进行处理,这样保障了采样请求组装能力应该是百万/秒左右的级别(直接从Map中取表达式计算、反射方式对象组装的结果,不快那就得有问题了),东西都做的那么细了,那么索性把UI上的事情也抠下细节:限制某些JTextField只能输入特定字符。例如:

  • 采样总数(SampleCount)只能为正整数;
  • 服务器选择权重(ServerChooseWeight)只能为整数(负数表示选择最快的接入点,正数表示强制指定某个接入点)
    如下图:
    在这里插入图片描述

2. IntegerTextField(整数输入框)

import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;

/**
 * IntegerTextField
 *
 * @author chenx
 */
public class IntegerTextField extends JTextField {

    public IntegerTextField() {
        super();
    }

    public IntegerTextField(int columns) {
        super(columns);
    }

    @Override
    protected Document createDefaultModel() {
        return new IntegerDocument();
    }

    /**
     * IntegerDocument
     */
    private static class IntegerDocument extends PlainDocument {

        @Override
        public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
            if (str == null) {
                return;
            }

            // 只允许输入数字、负号和删除键
            if (!str.matches("[\\d-]+") || (str.equals("-") && offs != 0)) {
                return;
            }

            super.insertString(offs, str, a);
        }
    }
}

3. PositiveIntegerTextField(正整数输入框)

import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;

/**
 * PositiveIntegerTextField
 *
 * @author chenx
 */
public class PositiveIntegerTextField extends JTextField {

    public PositiveIntegerTextField() {
        super();
    }

    public PositiveIntegerTextField(int columns) {
        super(columns);
    }

    @Override
    protected Document createDefaultModel() {
        return new PositiveIntegerDocument();
    }

    /**
     * PositiveIntegerDocument
     */
    private static class PositiveIntegerDocument extends PlainDocument {

        @Override
        public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
            if (str == null) {
                return;
            }

            // 只允许输入数字和删除键
            if (!str.matches("\\d+")) {
                return;
            }

            super.insertString(offs, str, a);
        }
    }
}

4. 总结

限制 JTextField 输入内容的原理涉及到对文本框中的文本进行监听和过滤。Java中,JTextField 使用 Document 对象来管理文本内容。Document 是一个抽象类,JTextField 默认使用 PlainDocument 的实例来存储文本。

限制输入内容的原理通常涉及两个步骤:
1、监听用户输入: 通过添加文本修改监听器(如 DocumentListener)或者拦截键盘输入事件(如 KeyListener),程序能够实时监测用户的输入行为。
2、过滤非法输入: 当监听到用户的输入时,程序会检查输入内容是否符合要求,如果不符合要求则阻止其插入到文本框中。这一步通常在文本插入方法(如 insertString())中进行处理。

在 Java Swing 中,你可以通过继承 PlainDocument 类并重写其 insertString() 方法来实现对输入内容的过滤和限制。在 insertString() 方法中,你可以编写逻辑来检查插入的文本是否符合规定,如果不符合则阻止其插入,这样就能实现限制输入内容的目的。

好吧,我承认这文章一堆废话,瞅两眼代码就啥都明白了,然而却叨叨了一堆。其实没别的,只是在想什么时候可以让《手撸IM专栏》https://blog.csdn.net/camelials/category_12602237.html初具规模,然后把基于它的JMeter通用MQTT采样器给开源出来,毕竟你搞一个IM 服务端的东西出来,不能没有Client去请求并验证它,否则难免有不能自圆其说的味道了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BossFriday

原创不易,请给作者打赏或点赞!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值