我正在创建客户端 - 服务器聊天应用程序,我打算需要存储用户名以获得更好的用户体验。一旦我触发主方法,就会调用load()方法。此方法通过从配置文件中读取来自动设置用户名。配置文件不为空(我已存储用户名)。但textField不更新。任何想法?这是我的加载方法:将文本设置为文本字段的问题
public static void load()
{
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("config.properties");
prop.load(input);
textField.setText(prop.getProperty("user")); //not updating!!!!
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
_config.properties_的内容是什么? – Berger
在这种情况下,从更简单的代码开始 - 用'textField.setText(“IsThisTextShown?”);''替换'load()'方法中的所有代码。然后,验证这是否成功 - 您会看到它是属性文件还是文本字段的问题。另外,为什么该方法“静态”?你应该添加一个[mcve]。 –
我这样做了。但仍然textField不更新。是否有任何害处使用静态?我通常这样做,因为它简化了我的编码过程。 –