gpt4 book ai didi

Java-MaskFormatter

转载 作者:行者123 更新时间:2023-11-30 06:31:54 24 4
gpt4 key购买 nike

有人能帮帮我吗……我已经尝试了各种方法(包括此处的帮助),但还是行不通。我将 JFormattedTextField 与 MaskFormatter 一起使用,以将数据输入限制为 4(最大)位数字。

    static JFormattedTextField textPayout;

MaskFormatter f;
try {
f = new MaskFormatter("####");
} catch (ParseException e) {
e.printStackTrace();
return; // actual code not written yet.
}
textPayout = new JFormattedTextField(f);

问题是它既不限制字符也不限制长度(另外,如果输入非数字,文本开始重叠)。而且我已经尝试了各种各样的类似面具的操作。有人可以告诉我我做错了什么吗?

谢谢

最佳答案

我刚刚尝试了这段代码,它运行良好,但有一个小问题:

class A extends JFrame {
public static void main(String args[]) throws ParseException {
A a = new A();
a.setLayout(new GridLayout());
JFormattedTextField textField =
new JFormattedTextField(new MaskFormatter("####"));
a.add(textField);
a.add(new JButton("qwe"));
a.setSize(300,50);
a.setVisible(true);
}
}

问题是最初出现的文本字段充满了 4 个空格,所以我不得不删除它们。可能这就是 Gentoo 编译的 IcedTea 7.2 的怪异之处。

否则一切正常,你能试试我的代码吗,如果它不起作用那么你的 Java 版本是多少?

关于Java-MaskFormatter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9344321/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com