JFormattedTextField未正确清除 [英] JFormattedTextField is not properly cleared

查看:103
本文介绍了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扩展JFormattedTextField更改为SudokuTextBox扩展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未正确清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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