我在用新数据更新JPanels时遇到问题。我正在开发游戏,我想做的基本概念是加载玩家并在JPanel中显示他持有的物品(以框式布局列出),我正在使用线程和循环来更新播放器项目。这对于一个玩家来说效果很好,但我的游戏允许其他玩家在其中切换。当我切换到下一个播放器时,我希望加载并更新他们的详细信息。这行不通。

这只是代码的一部分,但我希望有人能够看到我的问题。

    private JFrame frame;
 private JPanel mainPanel;
 private Box Item1 Item2, Item3;
 static private JPanel centerPanel;


 private boolean playerLoaded;
 private Player currentPlayer;

 private Thread gameThread;




 public void run(){

    while (running){



        for (Player player:allPlayers){
            playerLoaded = false;
            currentPlayer = player;
            player.setCurrentTurn(true);

            while (player.isCurrentTurn()){

                if (playerLoaded != true){

                    loadPlayer(player);
                    loadItems(player);
                    playerLoaded = true;


                }

                if ((playerLoaded == true){

                updateItems(player);

                try {
                    Thread.sleep(999);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                }

            }

        }


        }
    }


 public void loadPlayer(Player player){
jlCurrentPlayer.setText("Player: " + player.getName());

 }

 public void loadItems(Player player){
int count = 1;

 centerPanel = new JPanel(new GridLayout(1,3));

for (Item Item : player.getAllItems()){

    if (count == 1) {
    Item1 = Box.createVerticalBox();
    Item1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    JLabel jlItem = new JLabel(item.getName());
    Item1.add(jlItem);
    centerPanel.add(Item1);
    }
    else if (count ==2){
    Item2 = Box.createVerticalBox();
    Item2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    JLabel jlItem = new JLabel(item.getName());
    Item2.add(jlItem);
    centerPanel.add(Item2);
    }
    else if (count ==3){
    Item3 = Box.createVerticalBox();
    Item3.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    JLabel jlItem = new JLabel(item.getName());
    Item3.add(jlItem);
    centerPanel.add(Item3);
    }

    mainPanel.add(centerPanel,BorderLayout.CENTER);

    count++;
}

 }


  public void updateItemstats(Player player){
int count = 1;
if (player.isCurrentTurn()){
for (Item item : player.getAllItems()){

    if ((count == 1) && (player.isCurrentTurn())) {
        Item1.add(Box.createVerticalGlue());
        Item1.add(new JLabel("Value: " + item.getstats().getValue()));
        Item1.add(new JLabel("Quality: " + item.getStats().getQuality()));
    }
    else if (count ==2){
        Item2.add(Box.createVerticalGlue());
        Item2.add(new JLabel("Value: " + item.getstats().getValue()));
        Item2.add(new JLabel("Quality: " + item.getStats().getQuality()));

    }
    else if (count ==3){
        Item3.add(Box.createVerticalGlue());
        Item3.add(new JLabel("Value: " + item.getstats().getValue()));
        Item3.add(new JLabel("Quality: " + item.getStats().getQuality()));

    }
 count++;

}
}
 }

 JButton jbNext = new JButton("Next Player");

 jbNext.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            currentPlayer.setCurrentTurn(false);

        }
    });


对于我的主框架,我正在使用“边框布局”。当我切换播放器时发生的事情基本上是更新的信息,有时是播放器1和2的混合。

值25
品质60

价值50
质量20。

另外,如果玩家1有1个项目,而玩家2有3个项目,而我从玩家2切换到1,则有时玩家1将拥有所有玩家2的项目,而不是他自己的。

我认为这可能是Item1 2和3面板的问题,因此我尝试在下次播放器btn单击时将其删除,但这只是造成了异常。这很可能仍然是问题,我可能没有正确删除它们。

外面有人可以帮我吗?

最佳答案

您应该在下一个播放器动作监听器中为下一个播放器调用setCurrentTurn(true)

您是否已将currentPlayer中的Player变量设置为volatile?重要说明:private Player currentPlayerprivate boolean currentPlayer中的Player.java必须都为volatile。 (看看http://javamex.com/tutorials/synchronization_volatile.shtml

另外,使用Monitor和wait()代替Thread.sleep,这样您就可以使用notify()唤醒正在等待的Thread

以下是一些有关您代码的建议,但请注意,您将需要做更多的工作(例如,未显示的Player.java类中的代码)。

volatile List<Player> players = ...; // load your list of Player
volatile int currentPlayerIndex = 0;
volatile Player currentPlayer = null;
private final Object WAIT_MONITOR = new Object(); // yes, 'new Object()' !
[...]
public void run() throws InterruptedException {
    playerLoop:
    while(programRunning) { // maybe while(true)
        synchronized (WAIT_MONITOR) {
            loadPlayer(currentPlayer);
            loadItems(currentPlayer);

            updateItems(currentPlayer);
            WAIT_MONITOR.wait(1000);
        }
    }
}

[...]

JButton jbNext = new JButton("Next Player");

jbNext.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        synchronized(WAIT_MONITOR) { // this causes that the contents of run() and this method do not run at the same time.
            currentPlayer.setCurrentTurn(false);
            if(players.size() > currentPlayerIndex + 1) // don't use >=
                currentPlayerIndex++;
            else
                currentPlayerIndex = 0; // reset at the end of the list
            currentPlayer = players.get(currentPlayerIndex);
            currentPlayer.setCurrentTurn(true);
            WAIT_MONITOR.notifyAll(); // that causes the wait() method above to be continued.
        }
    }
});

10-02 02:26