java - 添加 KeyListener 后无法在文本字段中键入任何内容
问题描述
我正在做数独,但在将 KeyListener 添加到文本字段后,我无法在文本字段中键入任何内容。我的代码有什么问题?
我已经尝试过以下代码:
//add listener in the previous code
for (int row = 0; row < GRID_SIZE && !found; ++row) {
for (int col = 0; col < GRID_SIZE && !found; ++col) {
tfCells[row][col].addKeyListener(new KeyAction());
}
}
//implements the listener
private class KeyAction implements KeyListener {
public void keyPressed(KeyEvent ev){}
public void keyReleased(KeyEvent evt){}
public void keyTyped(KeyEvent e){
// Get the source object that fired the event
JTextField source = (JTextField)e.getSource();
// Scan JTextFileds for all rows and columns, and match with the source object
boolean found = false;
for (int row = 0; row < GRID_SIZE && !found; ++row) {
for (int col = 0; col < GRID_SIZE && !found; ++col) {
if (tfCells[row][col] == source) {
rowSelected = row;
colSelected = col;
found = true; // break the inner/outer loops
}
}
}
/* 1. Get the input String
2. Assume that the solution is unique. Compare the input number
with the number in the puzzle[rowSelected][colSelected].
If they are the same,set the background to green(Color.GREEN);
otherwise, set to red (Color.RED).
*/
String inputString = tfCells[rowSelected][colSelected].getText();
if(Integer.parseInt(inputString)==puzzle[rowSelected][colSelected]) {
tfCells[rowSelected[colSelected].setBackground(OPEN_CELL_TEXT_YES);
masksGen.setCellMasks(rowSelected, colSelected, false);
tfCells[rowSelected][colSelected].setEditable(false);
}else {
tfCells[rowSelected][colSelected].setBackground(OPEN_CELL_TEXT_NO);
}
//Check conflicts in row and col, the bg turn blue
for(int row = 0; row < GRID_SIZE; ++row){
if((tfCells[row][colSelected] == tfCells[rowSelected][colSelected]) && (row != rowSelected)){
tfCells[row][colSelected].setBackground(CONFLICT_BGCOLOR);
}
}
for(int col = 0; col < GRID_SIZE; ++col){
if((tfCells[rowSelected][col] == tfCells[rowSelected][colSelected]) && (col != colSelected)){
tfCells[rowSelected][col].setBackground(CONFLICT_BGCOLOR);
}
}
}
}
我希望听众在我输入数字时进行检查并做出响应。你能帮我找出为什么我不能输入任何东西吗?
解决方案
这似乎是罪魁祸首:
tfCells[rowSelected][colSelected].setEditable(false);
它阻止将字符输入到 JTextField 中,因为该字符将在键侦听器执行之后添加。因此,您输入的字符永远不会到达文本字段。
在提出一些建议之前,我冒昧地重新格式化了您的代码。
//add listener in the previous code
KeyAction action = new KeyAction();
for (int row = 0; row < GRID_SIZE; ++row) {
for (int col = 0; col < GRID_SIZE; ++col) {
tfCells[row][col].addKeyListener(action);
}
}
//implements the listener
protected class KeyAction extends KeyAdapter {
@Override
public void keyTyped(KeyEvent e){
// Get the source object that fired the event
JTextField source = (JTextField)e.getSource();
// Scan JTextFileds for all rows and columns, and match with the source object
FIND_FIELD:
for (int row = 0; row < GRID_SIZE; ++row) {
for (int col = 0; col < GRID_SIZE; ++col) {
if (tfCells[row][col] == source) {
rowSelected = row;
colSelected = col;
break FIND_FIELD;
}
}
}
/* 1. Get the input String
2. Assume that the solution is unique. Compare the input number
with the number in the puzzle[rowSelected][colSelected].
If they are the same,set the background to green(Color.GREEN);
otherwise, set to red (Color.RED).
*/
String inputString = tfCells[rowSelected][colSelected].getText();
if (Integer.parseInt(inputString)==puzzle[rowSelected][colSelected]) {
tfCells[rowSelected[colSelected].setBackground(OPEN_CELL_TEXT_YES);
masksGen.setCellMasks(rowSelected, colSelected, false);
tfCells[rowSelected][colSelected].setEditable(false);
} else {
tfCells[rowSelected][colSelected].setBackground(OPEN_CELL_TEXT_NO);
}
//Check conflicts in row and col, the bg turn blue
for (int row = 0; row < GRID_SIZE; ++row) {
if ((tfCells[row][colSelected] == tfCells[rowSelected][colSelected])
&& (row != rowSelected)){
tfCells[row][colSelected].setBackground(CONFLICT_BGCOLOR);
}
}
for (int col = 0; col < GRID_SIZE; ++col) {
if ((tfCells[rowSelected][col] == tfCells[rowSelected][colSelected])
&& (col != colSelected)) {
tfCells[rowSelected][col].setBackground(CONFLICT_BGCOLOR);
}
}
}
}
推荐阅读
- ruby-on-rails - 如何解决此错误“(pre:svcFault)服务故障”?
- javascript - 如何避免从 Outlook Web 插件载入打开的 Microsoft Graph API 同意的空白页面
- javascript - Google 工作表上的 Gmail 主题
- amazon-web-services - 解析参数“路径”时出错:无法检索
收到 403 aws S3 IAM 的非 200 状态代码 - react-native - 如何使用 react-native 截屏和分享?
- python - 有没有办法继续接收 Jupyter 代码单元中的输出,无意中转换为原始
- microsoft-graph-api - 使用 PATCH 在 OneNote 页面中更新创建日期
- javascript - '||' 的意外混合 和 '&&' 使用 babel 时
- reactjs - React Redux Props 在变量赋值期间未定义
- c++ - 快速图像 RGB 到 BGR 转换