2015-02-09 35 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

相關問題