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();

}

refreshScreen() が機能していないようです。それらは明らかにイベント ディスパッチャー スレッドで実行されており、加えられた変更はアクション リスナーの実行が終了したときにのみ表示されます。EDT 上にないため、MokkiGUI() コンストラクターから呼び出すと正常に動作します。

4

1 に答える 1

4

EDT スレッドをブロックしているため、イベント ディスパッチ ループが再描画イベントに到達できません。EDT の外部で実行して EDT とjava.awt.EventQueue.invokeLaterの通信に使用するか、javax.swing.Timer( java.util! ではなく) EDT でタスクを定期的に実行するために使用します。

于 2010-04-20T20:15:46.777 に答える