调用setText(“...”)时,TextField没有更新?

时间:2011-12-01 10:23:56

标签: java swing jpanel jtextfield

我目前正在java.In中开发MineSweeper,我必须更新JTextFields(如图所示)当我调用setText()时,Logger显示它被正确调用。但在输出中没有回应。

enter image description here

我的实际代码如下...

class GameInfoDisplayer{
          ...............
void setGameStatusText(String s){
    statusDisplayer.setStatus(s);
}
          ..............
 <....Definition of TimeController Inner class Comes here....>
    ..............
private class StatusDisplayer{
    JTextField displayField;
    Logger logger = Logger.getLogger("StatusDisplayer");
    StatusDisplayer(){
        displayField = new JTextField();
        gameInfoFieldsContainer.add(displayField);
    }
    void setStatus(String s){
            gameInfoFieldsContainer.remove(displayField);
            displayField.setText(s);        
            gameInfoFieldsContainer.add(displayField);
            gameInfoFieldsContainer.revalidate();
            gameInfoFieldsContainer.repaint();  
            logger.info("I am in updating Display content...:"+displayField.getText());
        }
    }

我做了所有重新绘制,重新验证similar question中给出但没有用...

1 个答案:

答案 0 :(得分:1)

尝试以下

private class StatusDisplayer{
    JTextField displayField;
    Logger logger = Logger.getLogger("StatusDisplayer");
    StatusDisplayer(){
        displayField = new JTextField();
        gameInfoFieldsContainer.add(displayField);
    }
    void setStatus(String s){
            displayField.setText(s);        
            logger.info("I am in updating Display content...:"+displayField.getText());
        }
    }
}