当用户在文本字段Java Swing中输入时屏蔽IP地址

时间:2014-02-26 12:06:02

标签: java swing jtextfield jformattedtextfield

我有一个JTextField来容纳一个3点的ip地址。 255.120.320.123。当用户输入此IP地址时,我想将其屏蔽为 我在引用这个帖子,How to custom formatter JFormattedTextField to display an IP address?

jFormattedTextField对我不起作用。任何人都可以给我一个jFormattedTextField示例,其中有3个点可见吗?

或者我是否需要使用此主题中提到的4 jFomattedTextField / JPasswordField

提前致谢。

2 个答案:

答案 0 :(得分:1)

似乎您需要使用MaskFormatter,例如:

try {
    MaskFormatter mf = new MaskFormatter("###.###.###.###");
    JFormattedTextField f = new JFormattedTextField(mf);
    add(f);
} catch (ParseException e) {
    e.printStackTrace();
}

enter image description here

答案 1 :(得分:0)

我假设您尝试使用JFormattedTextField?也许你应该把它与MaskFormatter结合起来。

像:       // IPv4如192.168.1.1

   MaskFormatter formatter = new MaskFormatter("###.###.###.###");
 JFormattedTextField textField = new JFormattedTextField(formatter);

Here is an Example and guide

相关问题