我的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");
    }
});

07-24 13:33