2015-05-21 130 views
0

我正在用AI创建一个棋盘游戏,我试图让AI选择将他的游戏片放入闪光灯一两秒钟,然后才能进行选定的棋步。我尝试如下:Java swing睡眠命令

的Thread.Sleep

button.setBackground(Color.pink); 
Thread.sleep(800); 
button.setBackground((new JButton()).getBackground()); 
Thread.sleep(800); 
button.setBackground(Color.pink); 
Thread.sleep(800); 
button.setBackground((new JButton()).getBackground()); 
Thread.sleep(800); 

主题

Thread flash = new Thread(new ButtonFlashThread(button)); 
flash.start(); 

赎回

ExecutorService es = Executors.newSingleThreadExecutor(); 
    Future<Boolean> task = es.submit(new ButtonFlashThread(button)); 

既与类(线程一个它实现Runnable并代替公共布尔()调用它是公共无效的run())

public class ButtonFlashThread implements Callable<Boolean> { 

private int sleepTime = 800; 
private JButton button; 

public ButtonFlashThread(JButton b) { 
    button = b; 
} 

public Boolean call() { 
    for (int i = 0; i < 3; i++) { 
     button.setBackground(Color.pink); 
     try { 
      Thread.sleep(sleepTime); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     button.setBackground((new JButton()).getBackground()); 
     try { 
      Thread.sleep(sleepTime); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    return false; 
} 

的invokeLater

Runnable flashON = new Runnable() { 
     public void run() { button.setBackground(Color.pink); } }; 
    Runnable flashOFF = new Runnable() { 
     public void run() { button.setBackground((new JButton()).getBackground()); } }; 
     javax.swing.SwingUtilities.invokeLater(flashON); 
     javax.swing.SwingUtilities.invokeLater(flashOFF); 
     javax.swing.SwingUtilities.invokeLater(flashON); 
     javax.swing.SwingUtilities.invokeLater(flashOFF); 

定时器

ActionListener flashON = new ActionListener(){ 
     public void actionPerformed(ActionEvent arg0){ button.setBackground(Color.pink); } }; 
    ActionListener flashOFF = new ActionListener(){ 
     public void actionPerformed(ActionEvent arg0) { button.setBackground((new JButton()).getBackground()); } }; 

    int sleep = 800; 
    Timer timer = new Timer(sleep, flashON); timer.setRepeats(false); timer.start(); 
    timer = new Timer(sleep, flashOFF); timer.setRepeats(false); timer.start(); 
    timer = new Timer(sleep, flashON); timer.setRepeats(false); timer.start(); 
    timer = new Timer(sleep, flashOFF); timer.setRepeats(false); timer.start(); 

Thread.sleep代码命令冻结GUI和所有其他尝试共在按钮闪烁的情况下用代码添加。不是我需要的。我想要按钮闪烁和代码暂停,直到按钮完成闪烁,然后才恢复。基本上我正在寻找一种方法来做到以下几点:

//Find desired move (done) 
//Flash the JButton a few times to indicate selected move 
//Do following move (done) 

按照这个顺序。

编辑1: 以下实现

private void flashButton(JButton button) {  
    class MoveTimerListener implements ActionListener { 
     private boolean flashOn = false; 
     private int count; 
     private int MAX_COUNT = 5; 
     private JButton button; 

     public MoveTimerListener(JButton b) { 
      button = b; 
     } 

     public void actionPerformed(ActionEvent e) { 
      flashOn = !flashOn; 
      if (count >= MAX_COUNT) { // MAX_COUNT is an int constant 
       // reached max -- stop timer 
       ((Timer) e.getSource()).stop(); 
       flashOn = false; 

       aiMove(); 
       updateGUI(); 
      } 

      if (flashOn) 
       button.setBackground(Color.yellow); 
      else 
       button.setBackground((new JButton()).getBackground()); 
      count++; 
     } 
    } 

    Timer t = new Timer(5000, new MoveTimerListener(button)); 
    t.start(); 
    updateGUI(); 
} 

没有停顿的代码,并让它继续在按钮仍然在后台闪烁,代码已经完成很久以后。

编辑2: 我实现你的例子是这样的:

private void flashButton() {   
    _moveTimer = new Timer(100, new MoveTimerListener()); 
    _moveTimer.start(); 
    updateGUI(); 
} 

private class MoveTimerListener implements ActionListener { 
    private boolean flashOn = false; 
    private int count; 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     flashOn = !flashOn; 
     if (count >= 20) { 
      ((Timer) e.getSource()).stop(); 
      flashOn = false; 
      _gm.makeMove(aiMove[0], aiMove[1]); 
     } 

     if (flashOn) { 
      _boardButtons[aiMove[0]][aiMove[1]].setBackground(Color.yellow); 
     } else { 
      _boardButtons[aiMove[0]][aiMove[1]].setBackground((new JButton()).getBackground()); 
     } 
     count++; 
    } 
} 

但它似乎代码总是跳过actionPerformed方法。我做错了什么?

编辑3:不是很小但可运行的程序。有问题的代码是在95-140行

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.util.LinkedList; 
import java.util.Random; 

import javax.swing.BoxLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.Timer; 

public class GameGUI extends JFrame implements MouseListener { 

private GameManager _gm; 

private JLabel[] _scores; 
private JLabel[] _players; 
private JPanel _contentPane; 
private JButton[] _toolbarButtons; 
private JButton[][] _boardButtons; 
private Timer _moveTimer; 
private int[] aiMove; 


public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       GameManager gm = new GameManager(); 
       //gm.addPlayer(new Player("player", 2, 0)); 
       gm.addPlayer(new Player("random", 2, 1)); 
       gm.addPlayer(new Player("max kills ai", 1, 2)); 
       gm.initBoard(8, 8); 
       GameGUI frame = new GameGUI(gm); 
       frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the frame. 
*/ 
public GameGUI(GameManager gm) { 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 450, 300); 
    _contentPane = new JPanel(); 
    _boardButtons = new JButton[gm.getBoardSize()[0]][gm.getBoardSize()[1]]; 
    _gm = gm; 
    addWindowGUI(_contentPane); 
    setContentPane(_contentPane); 
    aiMove(); 
    updateGUI(); 
} 

private void addWindowGUI(JPanel cp) { 
    cp.setLayout(new BoxLayout(_contentPane, BoxLayout.PAGE_AXIS)); 

    JPanel gameInfo = new JPanel(); 
    gameInfo.setLayout(new FlowLayout()); 

    //Game grid 
    JPanel gameGrid = new JPanel(); 
    int buttonSize = 40; 
    gameGrid.setLayout(new GridLayout(_gm.getBoardSize()[0], _gm.getBoardSize()[1])); 
    for (int i = 0; i < _boardButtons.length; i++) 
     for (int j = 0; j < _boardButtons[i].length; j++) { 
      _boardButtons[i][j] = new JButton(); 
      _boardButtons[i][j].setPreferredSize(new Dimension(buttonSize, buttonSize)); 
      _boardButtons[i][j].addMouseListener(this); 
      gameGrid.add(_boardButtons[i][j]); 
     } 
    gameInfo.add(gameGrid); 
    cp.add(gameInfo); 
} 

public void mouseClicked(MouseEvent e) { 
    if (!_gm.getCurrentPlayer().isAI()) { 
     //Find desired move and conduct it 
     for (int i = 0; i < _boardButtons.length; i++) 
      for (int j = 0; j < _boardButtons[i].length; j++) 
       if (e.getSource().equals(_boardButtons[i][j])) 
        _gm.makeMove(i,j); 

     aiMove(); 
     updateGUI(); 
    } 
} 

public void aiMove() { 
    int winner = _gm.isGameOver(); 
    while (winner == -1 && _gm.getCurrentPlayer().getAI() != 0) { 
     updateGUI(); 
     aiMove = _gm.getNextAIMove(); 
     flashButton(); 
     winner = _gm.isGameOver(); 
    } 

    if (winner != -1) { 
     // Winner 
    } 
} 

private void flashButton() {   
    _moveTimer = new Timer(100, new MoveTimerListener()); 
    _moveTimer.start(); 
    updateGUI(); 
} 

private class MoveTimerListener implements ActionListener { 
    private boolean flashOn = false; 
    private int count; 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     flashOn = !flashOn; 
     if (count >= 20) { 
      ((Timer) e.getSource()).stop(); 
      flashOn = false; 
      _gm.makeMove(aiMove[0], aiMove[1]); 
     } 

     if (flashOn) { 
      _boardButtons[aiMove[0]][aiMove[1]].setBackground(Color.yellow); 
     } else { 
      _boardButtons[aiMove[0]][aiMove[1]].setBackground((new JButton()).getBackground()); 
     } 
     count++; 
    } 
} 

public void mouseEntered(MouseEvent e) { 
    if (!_gm.getCurrentPlayer().isAI()) { 
     LinkedList<int[]> l = new LinkedList<int[]>(); 
     for (int i = 0; i < _boardButtons.length; i++) 
      for (int j = 0; j < _boardButtons[i].length; j++) 
       if (e.getSource().equals(_boardButtons[i][j])) 
        l = _gm.canPlace(i,j); 
     for (int i = 0; i < l.size(); i++) { 
      int[] temp = l.get(i); 
      _boardButtons[temp[0]][temp[1]].setBackground(Color.yellow); 
     } 
    } 
} 
private void updateGUI() { 
    for (int i = 0; i < _boardButtons.length; i++) 
     for (int j = 0; j < _boardButtons[i].length; j++) 
      if (_gm.getBoard().stoneAt(i, j) == 1) 
       _boardButtons[i][j].setBackground(Color.green); 
      else if (_gm.getBoard().stoneAt(i, j) == 2) 
       _boardButtons[i][j].setBackground(Color.red); 
} 
public void mouseExited(MouseEvent arg0) { updateGUI(); } 

//Not relevant 
public void mousePressed(MouseEvent arg0) { } 
public void mouseReleased(MouseEvent arg0) { } 


//Other classes 
public static class GameManager { 
    private int _turn; 
    private Player[] _players; 
    private GameBoard _board; 
    private GameLog _log; 

    public int getTurn() { return _turn; } 
    public Player[] getPlayers() { return _players; } 
    public GameBoard getBoard() { return _board; } 

    public GameManager() { 
     _turn = 0; 
     _players = new Player[0]; 
    } 

    public int[] getNextAIMove() { 
     AI ai = new AI(); 
     return ai.play(_players[_turn].getAI(), _board, _players[_turn].getColour()); 
    } 

    public void initBoard(int m, int n) { 
     int[] colours = new int[_players.length]; 
     for (int i = 0; i < colours.length; i++) 
      colours[i] = _players[i].getColour(); 
     _board = new GameBoard(m, n, colours); 
     updateScore(); 
    } 

    public void addPlayer(Player p) { 
     Player[] temp = _players; 
     _players = new Player[temp.length + 1]; 
     for (int i = 0; i < temp.length; i++) 
      _players[i] = temp[i]; 
     _players[temp.length] = new Player(p); 

     Random rnd = new Random(); 
     _turn = rnd.nextInt(_players.length); 
    } 

    private void advanceTurn() { 
     _turn++; 
     if(_turn == _players.length) 
      _turn = 0; 
    } 

    public void makeMove(int y, int x) { 
     if (_board.place(y, x, _players[_turn].getColour()) != 0) { 
      updateScore(); 
      advanceTurn(); 
     } 
    } 

    public LinkedList<int[]> canPlace(int y, int x) { 
     return _board.canPlace(y, x, _players[_turn].getColour()); 
    } 

    public int[] getBoardSize() { 
     return _board.getSize(); 
    } 

    private void updateScore() { 
     int[] scores = new int[_players.length]; 
     for (int i = 0; i < _board.getSize()[0]; i++) 
      for (int j = 0; j < _board.getSize()[1]; j++) 
       for (int j2 = 0; j2 < _players.length; j2++) 
        if (_players[j2].getColour() == _board.stoneAt(i, j)) 
         scores[j2]++; 
     for (int i = 0; i < _players.length; i++) { 
      _players[i].setScore(scores[i]); 
     } 
    } 

    public int isGameOver() { 
     int winner = 0; 
     for (int i = 0; i < _players.length; i++) { 
      for (int j = 0; j < _board.getSize()[0]; j++) 
       for (int j2 = 0; j2 < _board.getSize()[1]; j2++) 
        if (canPlace(j, j2).size() != 0) 
         return -1; 
      advanceTurn(); 
     } 
     for (int i = 1; i < _players.length; i++) 
      if (_players[i].getPoints() > _players[winner].getPoints()) 
       winner = i; 
     return winner; 
    } 
    public Player getCurrentPlayer() { 
     return _players[_turn]; 
    } 
} 

public static class AI { 
    public int[] play(int diff, GameBoard board, int colour) { 
     //List all possible moves 
     int[] move = new int[2]; 
     LinkedList<int[]> posibilities = new LinkedList<int[]>(); 
     for (int i = 0; i < board.getSize()[0]; i++) 
      for (int j = 0; j < board.getSize()[1]; j++) 
       if (board.canPlace(i, j, colour).size() > 0) { 
        int[] temp = {i, j}; 
        posibilities.add(temp); 
       } 

     //Choose a move according to AI difficulty 
     switch (diff) { 
     case 1: // Easy - random 
      Random r = new Random(); 
      move = posibilities.get(r.nextInt(posibilities.size())); 
      break; 

     case 2: // Medium - max kills 
      int max = 0, kills = board.canPlace(posibilities.get(0)[0], posibilities.get(0)[1], colour).size(); 
      for (int i = 1; i < posibilities.size(); i++) { 
       int[] temp = posibilities.get(i); 
       int tempkills = board.canPlace(temp[0], temp[1], colour).size(); 
       if (kills < tempkills) { 
        kills = tempkills; 
        max = i; 
       } 
      } 
      move = posibilities.get(max); 
      break; 

     case 3: // Hard 
      break; 
     } 

     return move; 
    } 
} 

public static class GameBoard { 
    private int[][] _board; 
    private int _m; 
    private int _n; 

    public GameBoard(int m, int n, int[] colours) { 
     _m = m; 
     _n = n; 
     _board = new int[m][n]; 
     for (int i = 0; i < _board.length; i++) 
      for (int j = 0; j < _board[i].length; j++) 
       _board[i][j] = 0; 
     if (_m % 2 == 0 && _n % 2 == 0) { 
      _board[m/2][n/2] = _board[m/2 - 1][n/2 - 1] = colours[0]; 
      _board[m/2][n/2 - 1] = _board[m/2 - 1][n/2] = colours[1]; 
     } 

     int[][] testBoard = { 
       {2, 2, 2, 2, 1, 1, 2, 2}, 
       {2, 1, 2, 2, 1, 1, 1, 2}, 
       {1, 1, 2, 2, 1, 2, 2, 1}, 
       {2, 2, 2, 1, 2, 1, 1, 1}, 
       {2, 2, 1, 2, 1, 1, 1, 2}, 
       {1, 1, 1, 2, 2, 2, 2, 2}, 
       {1, 1, 1, 2, 2, 2, 2, 2}, 
       {1, 1, 1, 2, 1, 1, 1, 0}}; 
     //_board = testBoard; 
    } 

    public int[] getSize() { 
     int[] size = {_m, _n}; 
     return size; 
    } 

    public LinkedList<int[]> canPlace(int y, int x, int colour) { 
     LinkedList<int[]> eats = new LinkedList<int[]>(); 
     if (_board[y][x] != 0) return eats; 
     int i; 
     if (existsAnotherIn("u", y, x, colour)) { 
      i = 1; 
      while (isInBounds(y + i, x) && _board[y + i][x] != colour && _board[y + i][x] != 0) { 
       int[] temp = {y + i, x}; 
       eats.add(temp); 
       i++; 
      } 
     } 
     if (existsAnotherIn("d", y, x, colour)) { 
      i = 1; 
      while (isInBounds(y - i, x) && _board[y - i][x] != colour && _board[y - i][x] != 0) { 
       int[] temp = {y - i, x}; 
       eats.add(temp); 
       i++; 
      }  
     } 
     if (existsAnotherIn("l", y, x, colour)) { 
      i = 1; 
      while (isInBounds(y, x - i) && _board[y][x - i] != colour && _board[y][x - i] != 0) { 
       int[] temp = {y, x - i}; 
       eats.add(temp); 
       i++; 
      } 
     } 
     if (existsAnotherIn("r", y, x, colour)) { 
      i = 1; 
      while (isInBounds(y, x + i) && _board[y][x + i] != colour && _board[y][x + i] != 0) { 
       int[] temp = {y, x + i}; 
       eats.add(temp); 
       i++; 
      } 
     } 
     if (existsAnotherIn("ul", y, x, colour)) { 
      i = 1; 
      while (isInBounds(y + i, x - i) && _board[y + i][x - i] != colour && _board[y + i][x - i] != 0) { 
       int[] temp = {y + i, x - i}; 
       eats.add(temp); 
       i++; 
      } 
     } 
     if (existsAnotherIn("ur", y, x, colour)) { 
      i = 1; 
      while (isInBounds(y + i, x + i) && _board[y + i][x + i] != colour && _board[y + i][x + i] != 0) { 
       int[] temp = {y + i, x + i}; 
       eats.add(temp); 
       i++; 
      } 
     } 
     if (existsAnotherIn("dl", y, x, colour)) { 
      i = 1; 
      while (isInBounds(y - i, x - i) && _board[y - i][x - i] != colour && _board[y - i][x - i] != 0) { 
       int[] temp = {y - i, x - i}; 
       eats.add(temp); 
       i++; 
      } 
     } 
     if (existsAnotherIn("dr", y, x, colour)) { 
      i = 1; 
      while (isInBounds(y - i, x + i) && _board[y - i][x + i] != colour && _board[y - i][x + i] != 0) { 
       int[] temp = {y - i, x + i}; 
       eats.add(temp); 
       i++; 
      } 
     } 
     return eats; 
    } 

    private boolean isInBounds(int i, int j) { 
     return i >= 0 && j >= 0 && i < _m && j < _n; 
    } 

    private boolean existsAnotherIn(String lane, int vo, int ho, int colour) { 
     int v = 0, h = 0; 

     do { 
      if ((v != 0 || h != 0) && _board[vo + v][ho + h] == colour) return true; 

      if (lane.contains("u"))   v++; 
      else if (lane.contains("d")) v--; 
      if (lane.contains("r"))   h++; 
      else if (lane.contains("l")) h--; 
     } while (isInBounds(vo + v, ho + h) && _board[vo + v][ho + h] != 0); 
     return false; 
    } 

    public int place(int y, int x, int colour) { 
     LinkedList<int[]> stonesToEat = canPlace(y, x, colour); 
     if (!stonesToEat.isEmpty()) { 
      _board[y][x] = colour; 
      for (int i = 0; i < stonesToEat.size(); i++) { 
       int[] place = stonesToEat.get(i); 
       _board[place[0]][place[1]] = colour; 
      } 
     } 
     return stonesToEat.size(); 
    } 

    public String toString() { 
     String s = ""; 
     for (int i = 0; i < _board.length; i++) { 
      for (int j = 0; j < _board[0].length; j++) { 
       s+=_board[i][j]+"\t"; 
      } 
      s += "\n"; 
     } 
     return s; 
    } 

    public int stoneAt(int i, int j) { 
     return _board[i][j]; 
    } 
} 

public static class Player { 
    private String _name; 
    private int _points; 
    private int _colour; 
    private int _ai; 

    public Player(String name, int colour, int ai) { 
     _name = name; 
     _points = 0; 
     _colour = colour; 
     _ai = ai; 
    } 

    public void setScore(int s) { 
     _points = s; 
    } 

    public Player(Player p) { 
     _name = p._name; 
     _points = p._points; 
     _colour = p._colour; 
     _ai = p._ai; 
    } 

    public boolean isAI() { 
     return _ai != 0; 
    } 

    public void addPoints(int add) { _points += add; } 

    public int getColour() { return _colour; } 

    public String getName() { return _name; } 

    public int getPoints() { return _points; } 

    public int getAI() { return _ai; } 
} 

public class GameLog extends GameManager { 
    private int _startingPlayer; 
    private LinkedList<String> _moves; 

    public GameLog(int startPlayer) { 
     _startingPlayer = startPlayer; 
     _moves = new LinkedList<String>(); 
    } 

    public void addMove(String m) { 
     _moves.add(m); 
    } 

    public int getStartingPlayer() { return _startingPlayer; } 

    public LinkedList<String> getMoves() { return _moves; } 
} 

}

回答

3

你在想这一点。只需创建单摇摆定时器,就是这样,并给它一个状态 - 即,给它的ActionListener字段改变,一个代表什么时候闪烁的布尔值表示你转换为真和假,另一个是一个int,它计算闪烁次数并在达到一定数字后关闭Timer,然后这会使得此举。

东西作为

class MoveTimerListener implements ActionListener { 
    private boolean flashOn = false; 
    private int count; 

    @Override 
    public void actionPerformed(ActionEvent e) { 
    flashOn = !flashOn; 
    if (count >= MAX_COUNT) { // MAX_COUNT is an int constant 
     // reached max -- stop timer 
     ((Timer) e.getSource()).stop(); 
     flashOn = false; 

     // TODO: place code to make move here 
    } 

    if (flashOn) { 
     // TODO: place code to flash here 
    } 
    count++; 
    } 
} 

总运行时间为简单,这将是MAX_COUNT *计时器的延迟。

例如,

enter image description here

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Graphics2D; 
import java.awt.GridLayout; 
import java.awt.RenderingHints; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 
import java.util.ArrayList; 
import java.util.List; 
import javax.swing.*; 

@SuppressWarnings("serial") 
public class FlashingTimerGui extends JPanel { 
    private static final int MAX_COUNT = 20; 
    private static final int IMG_WIDTH = 60; 
    private static final int SIDES = 3; 
    public static final int MOVE_TIMER_DELAY = 100; 
    private Icon blankIcon; 
    private Icon blackIcon; 
    private Icon redIcon; 
    private List<JLabel> labelList = new ArrayList<>(); 
    private Timer moveTimer; 
    private int labelListIndex = 0; 

    public FlashingTimerGui() { 
     blankIcon = createIcon(new Color(0, 0, 0, 0)); 
     blackIcon = createIcon(Color.black); 
     redIcon = createIcon(Color.red); 

     JPanel gridPanel = new JPanel(new GridLayout(SIDES, SIDES)); 
     gridPanel.setBorder(BorderFactory.createLineBorder(Color.black)); 
     for (int i = 0; i < SIDES * SIDES; i++) { 
     JLabel label = new JLabel(blankIcon); 
     label.setBorder(BorderFactory.createLineBorder(Color.black)); 
     gridPanel.add(label); 
     labelList.add(label); 
     } 

     JPanel buttonPanel = new JPanel(); 
     buttonPanel.add(new JButton(new MoveAction("Move"))); 

     setLayout(new BorderLayout()); 
     add(gridPanel, BorderLayout.CENTER); 
     add(buttonPanel, BorderLayout.PAGE_END); 

     labelList.get(labelListIndex).setIcon(blackIcon); 
    } 

    private Icon createIcon(Color color) { 
     BufferedImage img = new BufferedImage(IMG_WIDTH, IMG_WIDTH, 
      BufferedImage.TYPE_INT_ARGB); 
     Graphics2D g2 = img.createGraphics(); 
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
      RenderingHints.VALUE_ANTIALIAS_ON); 
     g2.setColor(color); 
     int x = 4; 
     int y = x; 
     int w = IMG_WIDTH - 2 * x; 
     int h = w; 
     g2.fillOval(x, y, w, h); 
     g2.dispose(); 
     return new ImageIcon(img); 
    } 

    private class MoveTimerListener implements ActionListener { 
     private boolean flashOn = false; 
     private int count; 

     @Override 
     public void actionPerformed(ActionEvent e) { 
     flashOn = !flashOn; 
     if (count >= MAX_COUNT) { // MAX_COUNT is an int constant 
      // reached max -- stop timer 
      ((Timer) e.getSource()).stop(); 
      flashOn = false; 

      // code to make move here 
      labelList.get(labelListIndex).setIcon(blankIcon); 

      labelListIndex++; 
      labelListIndex %= labelList.size(); 
      labelList.get(labelListIndex).setIcon(blackIcon); 
     } 

     if (flashOn) { 
      labelList.get(labelListIndex).setIcon(redIcon); 
     } else { 
      labelList.get(labelListIndex).setIcon(blackIcon); 
     } 
     count++; 
     } 
    } 

    private class MoveAction extends AbstractAction { 

     public MoveAction(String name) { 
     super(name); 
     int mnemonic = (int) name.charAt(0); 
     putValue(MNEMONIC_KEY, mnemonic); 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
     if (moveTimer != null && moveTimer.isRunning()) { 
      // timer is running -- ignore request to re-run 
      return; 
     } 

     moveTimer = new Timer(MOVE_TIMER_DELAY, new MoveTimerListener()); 
     moveTimer.start(); 
     } 

    } 

    private static void createAndShowGui() { 
     FlashingTimerGui mainPanel = new FlashingTimerGui(); 

     JFrame frame = new JFrame("Flashing Timer"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 

} 
+0

的代码仍然不暂停。我做了一个动作之后,AI立即执行一个动作,闪烁之后才会生效。 – iMax531

+0

@ iMax531:运行我的代码以查看我的意思。如果你仍然陷入困境,那么你将不得不创建和发布类似于我上面发布的内容:一个非常简单和小巧的自包含程序,可以为我们展示你的问题,[mcve](http: //stackoverflow.com/help/mcve)。 –

+0

由于某种原因,代码会跳过actionPerformed方法。编辑中的代码2 – iMax531