让 Swing 刷新 JLabel 时遇到问题(显然在事件调度线程上)

发布于 2024-08-29 18:34:01 字数 1835 浏览 12 评论 0原文

我有这个动作侦听器:

this.newGameButton.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent a) {
    MokkiGUI.this.game = newGameQuery();
    MokkiGUI.this.AI = new AIPlayer(MokkiGUI.this.game.getBoard());
    MokkiGUI.this.boardLabel.setText("");
    MokkiGUI.this.boardLabel.repaint();
    refreshScreen();
    JOptionPane.showMessageDialog(null, "Starting new game", "New game", JOptionPane.PLAIN_MESSAGE);
    if (MokkiGUI.this.game.getAIIndicator() % 2 == 1) {
      while (makeAIMove()) {
        MokkiGUI.this.refreshScreen();
      }
      MokkiGUI.this.refreshScreen();
    }
  }
});

public void refreshScreen() {
if (javax.swing.SwingUtilities.isEventDispatchThread()) {
  System.out.println("Is");
} else {
  System.out.println("Not");
}
MokkiGUI.this.boardLabel.setText(MokkiTest.printBoard(MokkiGUI.this.game.getBoard()));
MokkiGUI.this.boardLabel.repaint();
MokkiGUI.this.data.setText("X: " + MokkiGUI.this.game.getPlayer1name() + "\n Score: "
    + MokkiGUI.this.game.getPlayer1score() + "\n\n" + "O: " + MokkiGUI.this.game.getPlayer2name() + "\n Score: "
    + MokkiGUI.this.game.getPlayer2score());
MokkiGUI.this.data.repaint();
if (!MokkiGUI.this.game.redoable()) {
  MokkiGUI.this.forwardButton.setEnabled(false);
  MokkiGUI.this.allForwardButton.setEnabled(false);
} else {
  MokkiGUI.this.forwardButton.setEnabled(true);
  MokkiGUI.this.allForwardButton.setEnabled(true);
}
if (!MokkiGUI.this.game.undoable()) {
  MokkiGUI.this.backButton.setEnabled(false);
  MokkiGUI.this.allBackButton.setEnabled(false);
} else {
  MokkiGUI.this.backButton.setEnabled(true);
  MokkiGUI.this.allBackButton.setEnabled(true);
}
MokkiGUI.this.buttonPanel.repaint();

}

刷新屏幕()似乎不起作用。它们显然在事件调度程序线程上运行,并且所做的更改仅在操作侦听器执行结束时才会显示。从 MokkiGUI() 构造函数调用时它工作正常,因为它不在 EDT 上。

I have this action listener:

this.newGameButton.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent a) {
    MokkiGUI.this.game = newGameQuery();
    MokkiGUI.this.AI = new AIPlayer(MokkiGUI.this.game.getBoard());
    MokkiGUI.this.boardLabel.setText("");
    MokkiGUI.this.boardLabel.repaint();
    refreshScreen();
    JOptionPane.showMessageDialog(null, "Starting new game", "New game", JOptionPane.PLAIN_MESSAGE);
    if (MokkiGUI.this.game.getAIIndicator() % 2 == 1) {
      while (makeAIMove()) {
        MokkiGUI.this.refreshScreen();
      }
      MokkiGUI.this.refreshScreen();
    }
  }
});

public void refreshScreen() {
if (javax.swing.SwingUtilities.isEventDispatchThread()) {
  System.out.println("Is");
} else {
  System.out.println("Not");
}
MokkiGUI.this.boardLabel.setText(MokkiTest.printBoard(MokkiGUI.this.game.getBoard()));
MokkiGUI.this.boardLabel.repaint();
MokkiGUI.this.data.setText("X: " + MokkiGUI.this.game.getPlayer1name() + "\n Score: "
    + MokkiGUI.this.game.getPlayer1score() + "\n\n" + "O: " + MokkiGUI.this.game.getPlayer2name() + "\n Score: "
    + MokkiGUI.this.game.getPlayer2score());
MokkiGUI.this.data.repaint();
if (!MokkiGUI.this.game.redoable()) {
  MokkiGUI.this.forwardButton.setEnabled(false);
  MokkiGUI.this.allForwardButton.setEnabled(false);
} else {
  MokkiGUI.this.forwardButton.setEnabled(true);
  MokkiGUI.this.allForwardButton.setEnabled(true);
}
if (!MokkiGUI.this.game.undoable()) {
  MokkiGUI.this.backButton.setEnabled(false);
  MokkiGUI.this.allBackButton.setEnabled(false);
} else {
  MokkiGUI.this.backButton.setEnabled(true);
  MokkiGUI.this.allBackButton.setEnabled(true);
}
MokkiGUI.this.buttonPanel.repaint();

}

The refreshScreen()s don't seem to work. They are apprently running on the Event Dispatcher thread and the changes made only show up when the action listener's execution is over. It works fine when called from the MokkiGUI() constructor, as it is not on the EDT.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彩扇题诗 2024-09-05 18:34:01

您正在阻塞 EDT 线程,因此事件调度循环无法到达重绘事件。在 EDT 外部运行并使用 java.awt.EventQueue.invokeLater 与 EDT 通信,或者使用 javax.swing.Timer(而不是 java.util !) 在 EDT 上定期运行任务。

You are blocking the EDT thread, so it event dispatch loop can't get to the repaint events. Either run outside of the EDT and use java.awt.EventQueue.invokeLater to communicate with the EDT, or use javax.swing.Timer (not java.util!) to regularly run tasks on the EDT.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文