如何在更改的文本上动态更新 JLabel?

How to dynamically update JLabel on changed text?

我的 JLabel 以一个在绘制 JLabel 时为 0 的变量开始。另一个线程不断增加这个值,我想更新标签。

如果可能我不希望线程访问标签。 (gm.getPlayerByListIndex(index).getAlk()) 得到更新

    createPlayer.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            boolean checked = checkInputs(nameInput, difficultyInput);
            if (checked) {
                String playername = getInput(nameInput);
                addPlayerToMainWindow(gm.getRegisteredPlayers().size(), playername);
                gm.createPlayer(playername, Integer.parseInt(getInput(difficultyInput)));
                dialog.dispose();
                JLabel alkLabel = new JLabel(Integer.toString(gm.getPlayerByListIndex(index).getAlk()));
                window.getWindow().getContentPane().add(alkLabel);
                alkLabel.setVisible(true);
                alkLabel.setBounds(160, (120 * index) + 50, 100, 50);

            }
        }

    });

增加价值的帖子:

public class Dispenser extends Thread {

     List<Player> players = GameManager.getInstance().getRegisteredPlayers();

    public void run() {

       while (true) {
           System.out.println("alive");
           try {
               for (Player p : players) {
                   p.setAlk(p.getAlk() + p.getDifficulty()); // TODO too hard
               }
            } catch (ConcurrentModificationException e) {
                System.out.println("playerlist in use, retry");
            }
            try {
               sleep(1000); // TODO too short
            } catch (InterruptedException e1) {
               // TODO Auto-generated catch block
               e1.printStackTrace();
           }
        }
   }

}

在这种情况下,您可以使用 Observer pattern

你将需要和 Observer class 比如这个(注意: 这个实现假设 equalshashCode 是在Player class):

public class JLabelObserver{
   private JLabel label;
   private Player player;
   public JLabelObserver(JLabel label, Player player){
       this.label = label;
       this.player = player;
   }
   public update(Player player){
       if(this.player.equals(player))
           label.setText(player.getAlk())
   }
}

现在你的分配器 class 应该充当 Obervable class,所以你 Dispenser 看起来像这样:

public class Dispenser extends Thread {

    List<Player> players = GameManager.getInstance().getRegisteredPlayers();
    List<JLabelObserver> observers = new ArrayList<>();

    public void run() {

       while (true) {
           System.out.println("alive");
           try {
               for (Player p : players) {
                   p.setAlk(p.getAlk() + p.getDifficulty()); // TODO too hard
                   notifyObservers(p)
               }
            } catch (ConcurrentModificationException e) {
                System.out.println("playerlist in use, retry");
            }
            try {
               sleep(1000); // TODO too short
            } catch (InterruptedException e1) {
               // TODO Auto-generated catch block
               e1.printStackTrace();
           }
        }
   }

   public static addObserver(JLabelObserver observer){
       observers.add(observer);
   }

   public static notifyObservers(Player player){
        for(JLabelObserver observer : observers){
             observer.update(player);
        }
   }
}

最后,在您创建 JLabel 之后,您还创建了 Observer:

JLabel alkLabel = new JLabel(Integer.toString(gm.getPlayerByListIndex(index).getAlk()));
JLabelObserver alkLabelObserver = new JLabelObserver(alkLabel, index);
Dispenser.addObserver(alkLabelObserver);