2010-10-28 129 views
2

我試圖做出補救西蒙說喜歡使用遊戲鞦韆(JFrame中/ Jbuttons中/格設置)。目前我無法弄清楚如何獲得一系列閃光燈按鈕。由於actionPerformed是非靜態的,因此我無法通過擺動計時器運行線程。我不知道該怎麼辦。的Java Swing閃爍的按鈕序列

我會很感激,如果有人可以拿我的代碼一目瞭然,並提供洞察力。

package MemoryGame; 

import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 
import java.util.Random; 
import java.lang.InterruptedException; 

public class GUI { 

    static int seconds = 0; 
    static int buttonIndex = 0; 
    int[] movesArr = new int[10]; 
    int numberOfButtons = 18; 
    Thread thread = new Thread(); 
    boolean[] logicArr = new boolean[numberOfButtons]; 
    JFrame frame   = new JFrame("Memory Builder"); 
    JPanel buttonPanel = new JPanel(); 
    JPanel panelBot  = new JPanel(new BorderLayout()); 
    GridLayout grid  = new GridLayout(0, 3) {{ setHgap(8); setVgap(8); }}; 
    JLabel scoreLabel = new JLabel("Score"); 
    static JLabel timeLabel  = new JLabel("0"); 
    JButton button[]  = new JButton[numberOfButtons]; 
    Listener listener = new Listener(); 
    Timer displayTimer = new Timer(1000, listener.new DisplayTimeListener()); 
    Timer buttonTimer = new Timer(300, listener.new ButtonDisplayListener()); 
    Color buttonColor; 

    public GUI() { 

     //Setup frame 
     frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS)); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //Create button panel 
     buttonPanel.setLayout(grid); 
     buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 
     addButtonPanel(); 

     //create label pane 
     panelBot.add(timeLabel, BorderLayout.WEST); 
     panelBot.add(scoreLabel, BorderLayout.EAST); 
    } 

    public void centerFrame() { 

     Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 
     frame.setLocation((dim.width - frame.getWidth())/2, 
       (dim.height - frame.getHeight())/2); 
    } 

    public void addButtonPanel() { 

     for (int i=0; i < button.length; i++){ 
      button[i] = new JButton(); 
      button[i].addActionListener(listener.new ButtonListener()); 
      button[i].setText("Button " + (i + 1)); 
      button[i].setName(new Integer(i).toString()); 
      buttonPanel.add(button[i]); 
     } 

     buttonColor = button[0].getBackground(); 
    } 


    public void createGUI() { 
     frame.add(buttonPanel); 
     frame.add(new JSeparator()); 
     frame.add(panelBot);   
     frame.pack(); 
     centerFrame(); 
     buttonPanel.setVisible(true); 
     frame.setVisible(true); 
     displayTimer.start(); 
     runGame(); 
    } 

    public void runGame() { 

     boolean isGameOver = false; 
     int difficulty = 3; 
     Random random = new Random(); 

     //generate and display random sequence 
     for(int i = 0; i < difficulty; i++) { 
      movesArr[i] = random.nextInt(numberOfButtons); 
      System.out.println(movesArr[i]); 
     } 
     new BlinkThread().run(); 

    } 



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

    public class Listener { 


    boolean flag = true; 

    public class ButtonDisplayListener implements ActionListener { 

     public void actionPerformed(ActionEvent e) { 
      if (flag){ 
       button[buttonIndex].setBackground(Color.red); 
       flag = !flag; 
      } 
      else button[buttonIndex].setBackground(buttonColor); 

     } 
    } 

    public class DisplayTimeListener implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 
      GUI.timeLabel.setText(new Integer(seconds++).toString()); 
     } 
    } 

    public class ButtonListener implements ActionListener { 

     public void actionPerformed(ActionEvent e) { 
     } 
    } 

} 

class BlinkThread implements Runnable { 
    public void run(){ 

     for(int i: movesArr){ 
      try{ 
       Thread.sleep(200); 
       System.out.println("echo"); 
       button[i].setBackground(Color.RED); 
       Thread.sleep(200); 
       button[i].setBackground(buttonColor); 
      } 
      catch(Exception ex){} 
     } 

    } 
} 
} 

回答

3

您可以創建自己的線程(或者更確切地說,一個Runnable)是閃爍的按鈕和通話之間,例如睡覺

class FlashThread implements Runnable { 
    private int[] indices; 
    private int sleepTime; 
    ... suitable constructor 
    public void run() { 
     for (int i : indices) { 
      buttonOn(i); 
      Thread.sleep(sleepTime); 
      buttonOff(i); 
      Thread.sleep(sleepTime); 
     } 
    } 
} 

這應該做的伎倆。

或者,你可以創建一個定時器,並添加一個ActionListener它(類似於您正在使用ButtonDisplayListener等做什麼),做中閃爍。

順便說一句,不通過runGame每一次循環中創建一個新的隨機對象()。生成一次(最好作爲一個實例變量而不是本地變量),然後多次使用它。隨機對象的生成(可能)是昂貴的,並且生成一堆Random對象有些失敗了Random類的用途。

+0

謝謝you______________ – 2010-10-28 15:39:03

+0

我做你的跟帖說什麼,我認爲它的工作原理,但它顯示按鈕的面板之前確實閃爍的順序,我怎麼能解決這個問題(我更新的代碼) – 2010-10-28 15:58:08

3

正如@Cameron Skinner所建議的那樣,example使用javax.swing.Timer來閃爍按鈕以進行簡單的匹配遊戲。 Timer對此尤其方便,因爲其動作事件處理程序在事件分派線程上執行。