Jtextfield每3位数自动添加逗号

问题描述 投票:0回答:1

问候,我正在尝试创建一个曾经键入数字的jtextfield,它会自动添加逗号每3个数字,用户类型3000000和jtextfield显示:3.000.000

这就是我尝试但没有作用的东西。

JFormattedTextField nameOfTextField = new JFormattedTextField(NumberFormat.getNumberInstance(Locale.ENGLISH));
                textField_10 = new JFormattedTextField(nameOfTextField);
                textField_10.addKeyListener(new KeyAdapter() {
                    @Override
                    public void keyTyped(KeyEvent evt) {
                        char c = evt.getKeyChar();
                        if(Character.isLetter(c) && !evt.isAltDown()){
                            evt.consume();

                        }
                    }
                });

                textField_10.setColumns(10);
                textField_10.setBounds(289, 133, 83, 20);
                panel_6.add(textField_10);
java jtextfield
1个回答
0
投票

JFormattedTextField已经这样做了。你不需要做任何特别的事情。你甚至不需要KeyListener。这就足够了:

JFormattedTextField nameOfTextField = new JFormattedTextField(NumberFormat.getNumberInstance(Locale.ENGLISH));
//textField_10 = new JFormattedTextField(nameOfTextField);      // This will not compile.  There is no such constructor.
textField_10 = nameOfTextField;

textField_10.setColumns(10);
© www.soinside.com 2019 - 2024. All rights reserved.