未正确清除JFormattedTextField我正在做这个任务,做一个解决sudoku的程序。我有一个带有SudokuTextBox扩展JFormattedTextField的网格的面板。我有一个MaskForMatter,所以它每个文本框只接受一个整数。然后,在我的面板中,当一个键被重命名时,我就有了这个代码。 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();
}问题是,如果我在文本框中放置了一个有效值,然后返回并输入一个无效的值(根据sudoku的规则),文本框将被清除。但是,当我转发选项卡时,前面的有效值将显示在文本框中。我的sudokumatrix包含了输入的所有数字,所以它只在相应的文本框中清除了应该的值。当我将“SudokuTextBox扩展了JFormattedTextField”改为“SudokuTextBox扩展了JTextField”时,事情变得更加混乱。但是我不能设置JTextField的大小,使它是正方形的,并且不能强制每个文本框只有一个整数。我错过了什么很明显的东西吗?
3 回答

互换的青春
TA贡献1797条经验 获得超6个赞

30秒到达战场
TA贡献1828条经验 获得超6个赞
public class stareOpsIndexListener extends KeyAdapter implements FocusListener { public void focusGained(FocusEvent e) { } public void focusLost(FocusEvent e) { try { JFormattedTextField jftf = (JFormattedTextField) e.getComponent(); jftf.commitEdit(); updateStareOpsIndex(jftf); } catch (ParseException ex) { jtfStarePatReq.setText(""); jtfStarePatFound.setText(""); jftfStareOpsIndex.setValue(null); } } public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_ENTER) { try { JFormattedTextField jftf = (JFormattedTextField) e.getComponent(); jftf.commitEdit(); updateStareOpsIndex(jftf); } catch (ParseException ex) { jtfStarePatReq.setText(""); jtfStarePatFound.setText(""); jftfStareOpsIndex.setValue(null); } } }}
添加回答
举报
0/150
提交
取消