设置java gui输入的掩码百分比



我在java gui中有一个JTextField我希望所有的值都是这样的:nums.nums%

实际上,我希望它能一直加到我的双数上,百分比符号。

我试过这样做:

MaskFormatter mf1 = new MaskFormatter("###.##");
mf1.setPlaceholderCharacter('%');
JFormattedTextField field = new JFormattedTextField(mf1);

但它看起来是这样的:%%%.%%我想要如果我写55它写55%

我还试着使用

MaskFormatter mf1 = new MaskFormatter("###.##%");
JFormattedTextField field = new JFormattedTextField(mf1);

这很好,但问题是它阻止我输入很多这样的数字:

45345345345.44%

它只给我三个这样的数字:123.12%我想要有时1.44%, 22.55%, 223.55%, 45345.12% ....

tnx很多。。。

这应该有效:

textfield.addFocusListener( e -> {
  @Override
  public void focusLost( final FocusEvent e )
  {
    JTextField textfield = (JTextField) e.getSource();
    textfield.setText(textfield.getText() + "" + "%");
  }
});

或者至少我会这么做。我还没试过。

最新更新