首页 > 解决方案 > 添加 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);
        }
    }

     }
}

我希望听众在我输入数字时进行检查并做出响应。你能帮我找出为什么我不能输入任何东西吗?

标签: javaswing

解决方案


这似乎是罪魁祸首:

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);
            }
        }
    }
}

推荐阅读