我的 GUI 锁定是因为我需要通过 EDT 更新它,但是,我还需要传递一个正在使用 GUI 更新的变量:
while ((message = this.in.readLine()).startsWith("NUMPLAYERS"))
{
numOfPlayers = Integer.parseInt(message.split(":")[1]);
numPlayers.setText("There are currently " + numOfPlayers + " players in this game");
}
这是行不通的。我需要在 EDT 中设置文本,但我不能将 numOfPlayers 传递给它而不声明它是最终的(我不想这样做,因为它随着新玩家加入服务器而改变)
最佳答案
最简单的解决方案是使用 final
临时变量:
final int currentNumOfPlayers = numOfPlayers;
EventQueue.invokeLater(new Runnable() {
public void run() {
numPlayers.setText("There are currently " +
currentNumOfPlayers + " players in this game");
}
});
关于java - 将变量传递给事件调度线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/753884/