JTextField清除后停止使用MaskFormatter [英] JTextField stops using MaskFormatter after being cleared

查看:137
本文介绍了JTextField清除后停止使用MaskFormatter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个Sudoku求解器,为此,我希望我的JTextFields只接受数字123456789中的一个作为有效输入.因此,我将MaskFormatter与JFormattedTextField一起使用.但是,当我通过执行.setText(")清除所有TextField时,MaskFormatter不再起作用.清除文本框后,我可以再次在其中写入任何内容.为什么以及如何解决?

I'm doing a Sudoku solver and for that I want my JTextFields to only accept one of the numbers 123456789 as valid input. Therefore I use a MaskFormatter toghether with a JFormattedTextField. However when I clear all the TextFields by doing .setText("") the MaskFormatter doesn't work anymore. After clearing the textboxes I can write anything in them again. Why and how do I fix it?

我的代码基本上是:

MaskFormatter formatter = new MaskFormatter("#");
formatter.setValidCharacters("123456789");
Font textFieldFont = new Font("Verdana", Font.BOLD, 30);
for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++) {
        southPanel.setBorder(lineBorder);
        field[i][j] = new JFormattedTextField(formatter);
        field[i][j].setHorizontalAlignment(JTextField.CENTER);
        field[i][j].setFont(textFieldFont);
        southPanel.add(field[i][j]);
    }
}

然后当我清除它时:

for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++) {
        field[i][j].setText("");
    }
}

这是所有代码,尚未编写大部分代码,因为我的朋友做到了.我现在要接管一下GUI的修复.

Here is all the code, haven't written most of it cuz my friend did it. I'm just now taking over to fix the GUI a little bit.

http://dl.dropbox.com/u/4018313/SudokuSolver.zip

此外,经过更多测试后,清除所有框后,您似乎可以键入很多不应该存在的字符,但是当您单击另一个字段时,所有字符都将消失.然后,如果您单击其他框,则会显示您先前写的数字.

Also, after some more testing it seems like after clearing all the boxes you can type a lot of charachters that should not be there but when you click on another field all of them will disappear. Then if you click in the other boxes the numbers you wrote earlier will appear.

不要得到这个!

推荐答案

我无法告诉您确切的原因,但是setText似乎使您的JFormattedTextField疯狂,因为""是一个字符串,并且与当前的蒙版.

I can't tell you the exact reason but setText seems to drive your JFormattedTextField crazy because "" is a String and it is against the current mask.

请尝试改用setValue(null).

我已经确保此方法有效.下一段代码证明了这一点:

I've just made sure that this method works. The next piece of code proves it:

public class Two extends JFrame {

    public static void main(String[] args) throws Exception {
        new Two().a();
    }

    void a() throws Exception {
        this.setLayout(new GridLayout(2, 1));
        MaskFormatter formatter = new MaskFormatter("#");
        formatter.setValidCharacters("123456789");
        final JFormattedTextField field = new JFormattedTextField(formatter);
        JButton b = new JButton("null!");
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                field.setValue(null);
            }
        });
        this.add(field);
        this.add(b);
        this.setSize(100, 100);
        this.setVisible(true);
    }
}

单击 null!按钮格式化程序后,它应会继续工作.

After clicking the null! button formatter continues to work as it is supposed to work.

这篇关于JTextField清除后停止使用MaskFormatter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆