本文介绍了JFormattedTextField 未正确清除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做这个作业,做一个解决数独的程序.我有一个带有 SudokuTextBox 网格的面板扩展 JFormattedTextField.我有一个 MaskFormatter,因此每个文本框只接受一个整数.然后在我的面板中,当一个键被释放时,我有这个代码.

I am doing this assignment, make a program that solves sudoku. I have a panel with a grid of SudokuTextBox extends JFormattedTextField. I have a MaskFormatter so that it only accepts one integer per text box.Then in my panel I have this code when a key is relesed.

 public void keyReleased(KeyEvent e) {
  SudokuTextBox tb = (SudokuTextBox) e.getSource();
  int row = tb.getRow();
  int col = tb.getCol();
  int value = toInteger(tb.getText());
  //System.out.println(value);
  if(sudoku.isValid(row, col, value)) {
   sudoku.set(row, col, value);
  }
  else {
   sudoku.set(row, col, 0);
   tb.setText(null);
  }
  tb.setCaretPosition(0);
  sudoku.print();
 }

问题是,如果我在文本框中输入一个有效值,然后我返回并输入一个无效值(根据数独规则),文本框将被清除.但是当我向前跳时,前一个有效值显示在文本框中.我的 sudokumatrix 包含已输入的所有数字,它确实清除了该值,因此它只出现在相应的文本框中.

The thing is, if i put a valid value in a text box, then i go back and enter an invalid value (by the rules of sudoku) the text box is cleared. But then when I tab forward the previous valid value is displayed in the text box.My sudokumatrix that contains all the numbers that has been inputed do clear the value like it should so it is only in the corresponding text box.

当我将SudokuTextBox extends JFormattedTextField"更改为SudokuTextBox extends JTextField"时,让事情变得更加混乱,它就像一个魅力.但是我无法将 JTextField 的大小设置为正方形,并且我不能强制每个文本框只使用一个整数.

To make matters even more confusing when i change "SudokuTextBox extends JFormattedTextField" to "SudokuTextBox extends JTextField" it works like a charm. But i cant set the size of JTextField so that it is square and I can not enforce only one integer per text box.

我是否遗漏了一些非常明显的东西?

Am I missing something really obvious?

推荐答案

好的,现在我找到了,掩码格式化程序的缺点之一是,截至当前的实现(Java 5),它不支持让用户在任何时候离开字段后,将字段恢复为空白值(在任何用户输入之前字段的初始值)."

Ok so now I found it, "One of the shortcomings of the mask formatter is that as of the current implementation (Java 5), it has no support for letting a user revert a field to the blank value (the initial value of the field prior to any user input) once they have left the field at any point."

因此,由于我使用的是 MaskFormatter,因此无法清除该字段.

So since I am using a MaskFormatter I cannot clear the field.

这篇关于JFormattedTextField 未正确清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 20:14