2015-02-09 90 views
0

我完全知道很少有人问过之前的非常类似的问题。我试图实施提供的解决方案 - 徒劳无功。 ...我面临的问题是闪烁按钮ONE AFTER ANOTHER。我可以做一个,但是当把闪烁的顺序放在一个循环中时 - 一切都会中断。任何帮助到Java的新人表示赞赏。附:我不允许使用线程。什么我现在有是:java Swing timer一个接一个地执行几项任务

Timer colorButton = new Timer(1000, new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 

      for (int i = 0; i < pcArray.length; i++) { 
       playSquare = pcArray[i]; 
       System.out.println("PlaySquare " + playSquare); 

       if (playSquare == 1) { 
        if (alreadyColoredRed) { 
         colorBack.start(); 
         colorButton.stop(); 
        } else { 
         red.setBackground(Color.red); 
         alreadyColoredRed = true; 
         System.out.println("RED DONE"); 
        } 
       } else if (playSquare == 2) { 
        if (alreadyColoredGreen) { 
         colorBack.start(); 
         colorButton.stop(); 
        } else { 
         green.setBackground(Color.green); 
         alreadyColoredGreen = true; 
         System.out.println("GREEN DONE"); 
        } 

       } 
      } 

     } 
    }); 
    Timer colorBack = new Timer(1000, new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 

      for (int i = 0; i < pcArray.length; i++) { 
       playSquare = pcArray[i]; 
       System.out.println("PlaySquare " + playSquare); 

       if (playSquare == 1) { 
        red.setBackground(Color.gray); 
        alreadyColoredRed = false; 
        System.out.println("RED PAINTED BACK"); 
        colorBack.stop(); 
       } else if (playSquare == 2) { 
        green.setBackground(Color.gray); 
        alreadyColoredGreen = false; 
        System.out.println("GREEN PAINTED BACK"); 
        colorBack.stop(); 
       } 
      } 

     } 
    }); 
+0

为了更好地帮助越早,张贴[MCVE](http://stackoverflow.com/help/mcve)(最小完备可验证实施例)或[SSCCE](HTTP:// WWW .sscce.org /)(简短,独立,正确的例子)。 – 2015-02-09 21:38:35

+1

*“我完全意识到非常相似的问题..”*如果你与他们联系起来,我们也会完全意识到他们。那么做怎么样? – 2015-02-09 21:40:30

+0

谢谢你,安德鲁。将试图找出这种方式 – KTM950 2015-02-09 21:59:42

回答

3

我不认为有两个Timer实例是要走的路。摇摆Timer是臭名昭着的'漂'远离完美的节拍一段时间。

更好地创建一个具有控制所有操作所需逻辑的计时器。

E.G.显示国际象棋骑士的允许移动。

enter image description here

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.*; 

public class KnightMove { 

    KnightMove() { 
     initUI(); 
    } 

    ActionListener animationListener = new ActionListener() { 

     int blinkingState = 0; 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      final int i = blinkingState % 4; 
      chessSquares[7][1].setText(""); 
      chessSquares[5][0].setText(""); 
      chessSquares[5][2].setText(""); 
      switch (i) { 
       case 0: 
        setPiece(chessSquares[5][0], WHITE + KNIGHT); 
        break; 
       case 1: 
       case 3: 
        setPiece(chessSquares[7][1], WHITE + KNIGHT); 
        break; 
       case 2: 
        setPiece(chessSquares[5][2], WHITE + KNIGHT); 
      } 
      blinkingState++; 
     } 
    }; 

    public void initUI() { 
     if (ui != null) { 
      return; 
     } 

     ui = new JPanel(new GridLayout(8, 8)); 
     ui.setBorder(new CompoundBorder(new EmptyBorder(4, 4, 4, 4), 
       new LineBorder(Color.BLACK,2))); 

     boolean black = false; 
     for (int r = 0; r < 8; r++) { 
      for (int c = 0; c < 8; c++) { 
       JLabel l = getColoredLabel(black); 
       chessSquares[r][c] = l; 
       ui.add(l); 
       black = !black; 
      } 
      black = !black; 
     } 
     for (int c = 0; c < 8; c++) { 
      setPiece(chessSquares[0][c], BLACK + STARTING_ROW[c]); 
      setPiece(chessSquares[1][c], BLACK + PAWN); 
      setPiece(chessSquares[6][c], WHITE + PAWN); 
      setPiece(chessSquares[7][c], WHITE + STARTING_ROW[c]); 
     } 

     Timer timer = new Timer(750, animationListener); 
     timer.start(); 
    } 

    private void setPiece(JLabel l, int piece) { 
     l.setText("<html><body style='font-size: 60px;'>&#" + piece + ";"); 
    } 

    private final JLabel getColoredLabel(boolean black) { 
     JLabel l = new JLabel(); 
     l.setBorder(new LineBorder(Color.DARK_GRAY)); 
     l.setOpaque(true); 
     if (black) { 
      l.setBackground(Color.GRAY); 
     } else { 
      l.setBackground(Color.WHITE); 
     } 
     return l; 
    } 

    public JComponent getUI() { 
     return ui; 
    } 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(
          UIManager.getSystemLookAndFeelClassName()); 
       } catch (Exception useDefault) { 
       } 
       KnightMove o = new KnightMove(); 

       JFrame f = new JFrame("Knight Moves"); 
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       f.setLocationByPlatform(true); 

       f.setContentPane(o.getUI()); 
       f.setResizable(false); 
       f.pack(); 

       f.setVisible(true); 
      } 
     }; 
     SwingUtilities.invokeLater(r); 
    } 

    private JComponent ui = null; 
    JLabel[][] chessSquares = new JLabel[8][8]; 
    public static final int WHITE = 9812, BLACK = 9818; 
    public static final int KING = 0, QUEEN = 1, 
      ROOK = 2, KNIGHT = 4, BISHOP = 3, PAWN = 5; 
    public static final int[] STARTING_ROW = { 
     ROOK, KNIGHT, BISHOP, KING, QUEEN, BISHOP, KNIGHT, ROOK 
    }; 
} 
+1

一个相关的例子,通过一个'队列'周期看到[这里](http://stackoverflow.com/a/24718561/230513)。 – trashgod 2015-02-09 22:46:55

+0

如果您认为上面的代码很可爱,请参阅['在标签中填充Unicode字符](http://stackoverflow.com/q/18686199/418556)&[制作强大的可调整大小的Swing Chess GUI](http: //stackoverflow.com/questions/21142686/making-a-robust-resizable-swing-chess-gui).. – 2015-02-09 23:39:25

+1

请[接受答案](http://meta.stackexchange.com/a/5235/155831)如果它有助于解决问题。 – 2015-02-16 07:43:50

相关问题