日期:2014-05-20  浏览次数:20748 次

我想哭。。。。续:我不想哭,但我有问题要请教大家!!!!!!!!!!
一个输入IP地址的文本框,加了一些格式限制后,如何读取输入的IP地址呢?
我用ip.getValue()取出来是null;用ip.getText()取出来是___-___-___-___
代码如下:
JLabel label7 = new JLabel("IP:*");
label7.setBounds(30, 252, 59, 17);
getContentPane().add(label7);
MaskFormatter mf1=null;
try {
mf1 = new MaskFormatter("###-###-###-###");
} catch (ParseException e1) {e1.printStackTrace();}
mf1.setPlaceholderCharacter('_');
final JFormattedTextField ip = new JFormattedTextField(mf1);
ip.setBounds(126, 252, 150, 22);
getContentPane().add(ip);

------解决方案--------------------
Java code
JFormattedTextField ftf = (JFormattedTextField)input;
             AbstractFormatter formatter = ftf.getFormatter();
             if (formatter != null) {
                 String text = ftf.getText();
                 try {
                      formatter.stringToValue(text);
return true;
                  } catch (ParseException pe) {
                      return false;
                  }

------解决方案--------------------
探讨

引用:

Java code
JFormattedTextField ftf = (JFormattedTextField)input;
AbstractFormatter formatter = ftf.getFormatter();
if (formatter != null) {
String text = ft……

JFormat……

------解决方案--------------------
探讨

AbstractFormatter formatter = ftf.getFormatter();中的AbstractFormatter可以是自己定义的格式?比如
MaskFormatter mf1=null;
try {
mf1 = new MaskFormatter("###-###-###-###");
} catch (ParseException e1) {e1.printSt……

------解决方案--------------------
探讨

我犯了一个小学生都不会犯的错误,唉!

我发现一个问题当我输入上网IP是12位数字时(如192.168.132.110),ip.getText()可以取到值,当少于12位时,就取不到。