2016-12-07 79 views
0

我正在製作一個Simon說的程序,並且當生成隨機模式時我需要按鈕閃爍,以便用戶知道要輸入什麼。我的問題是,我無法讓我的按鈕(圖像的JButton)閃爍,我的邏輯是有兩個按鈕在另一個之上,一個可見和不可見,然後切換按鈕的可見性,等待一秒鐘,然後將其更改回來。我試過使用Thread.sleep(),wait(),甚至是繁忙的循環來等待,但沒有任何工作。我被告知揮杆計時器是我最好的選擇,這就是我想要使用的。另外,我想讓按鈕在點擊開始按鈕後開始閃爍。Simon說的閃爍按鈕

public class GamePanel extends JPanel implements ActionListener 
{ 
    private JButton greenButton; 

    private JButton startButton; 
    private JButton greenBlinkButton; 
    private GridBagConstraints gbc; 
    private Timer buttonTimer; 

GamePanel() 
{ 
    gbc = new GridBagConstraints(); 
    GridBagLayout grid = new GridBagLayout(); 
    setLayout(grid); 

    //GridBag location lets 
    startButton = new JButton("Start"); 
    gbc.gridx = 0; 
    gbc.gridy = 0; 
    add(startButton, gbc); 
    startButton.setPreferredSize(new Dimension(200,30)); 

    greenReg = new ImageIcon("src/Images/Green Button.png"); 
    greenBlink = new ImageIcon("src/Images/Blink Green Button.png"); 

    greenButton = new JButton(new ImageIcon(greenReg)); 

    gbc.gridx = 0; 
    gbc.gridy = 1; 
    add(greenButton, gbc); 
    add(greenBlinkButton, gbc); 


    startButton.addActionListener(this); 


    //Timer 
    buttonTimer = new Timer(500, this); 

} 

@Override 
    public void actionPerformed(ActionEvent e) 
    {  

    if(e.getSource() == startButton) 
    { 

     greenButton.setIcon(greenBlink); 
     buttonTimer.start(); 
     greenButton.setIcon(greenReg); 
    } 
    } 

這是第一個讓它簡單的按鈕的代碼。

+1

是,使用一個Swing計時器,沒有,不換按鈕。交換圖標顯示的按鈕 - ***很多***更簡單和白癡證明。所有你需要做的就是調用定時器內的按鈕上的'setIcon(...)'。 –

+1

[不使用JButton的Simon遊戲的另一個例子,但可能有用](http://stackoverflow.com/questions/33027235/how-to-make-keypress-work-with-keylistener)我。 –

+0

@HovercraftFullOfEels謝謝生病嘗試使用它,但我的主要問題是使用計時器,如果你可以提供任何煽動 – McD

回答

0

您似乎試圖在自己的偵聽器中重新啓動Timer,這是您不應該這樣做的。瞭解該偵聽器中的actionPerformed方法將每隔500毫秒重複調用一次,並且無需嘗試重​​新啓動定時器,因爲它已經在運行。相反,在actionPerformed方法中,您應該有代碼來確定哪個按鈕應該更改圖標/狀態,以及要放置哪個圖標。

例如,請考慮下面的代碼。它假定有兩個圖標,一個名爲greenIcon,一個名爲darkGreenIcon,我希望每200毫秒交換一次這些圖標。在我的定時器的actionPerformed中,我將通過調用按鈕上的getIcon()來查看按鈕當前顯示哪個圖標。如果是greenIcon,我會通過setIcon(...)將darkGreenIcon放入按鈕中,反之亦然,如果它是darkGreenIcon,我會放入一個greenIcon。該代碼可能看起來是這樣的:

@Override 
    public void actionPerformed(ActionEvent e) { 
     // get icon from button 
     Icon icon = greenButton.getIcon(); 

     // check if it's the green icon 
     if (icon == greenIcon) { 
      icon = darkGreenIcon; // if so, make it the dark green icon 
     } else { 
      icon = greenIcon; // if not, make it the green icon 
     } 
     greenButton.setIcon(icon); // stuff it back into the button 
    } 

例如,可運行程序:

import java.awt.BasicStroke; 
import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Graphics2D; 
import java.awt.RenderingHints; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 
import javax.swing.*; 

@SuppressWarnings("serial") 
public class FlashingButton extends JPanel { 
    private static final String START = "Start"; 
    private static final String STOP = "Stop"; 
    private static final int TIMER_DELAY = 200; // millisecond delay 
    private static final int BI_WIDTH = 400; 
    private Icon greenIcon; 
    private Icon darkGreenIcon; 
    private JButton greenButton = new JButton(); 
    private JButton startButton = new JButton(new StartAction(START)); 
    private Timer timer = new Timer(TIMER_DELAY, new TimerListener()); 

    public FlashingButton() { 
     greenIcon = createMyIcon(Color.GREEN); 
     darkGreenIcon = createMyIcon(Color.GREEN.darker()); 
     greenButton.setIcon(greenIcon); 
     setBorder(BorderFactory.createEmptyBorder(40, 40, 40, 40)); 
     setLayout(new BorderLayout(20, 20)); 

     add(greenButton, BorderLayout.CENTER); 
     add(startButton, BorderLayout.PAGE_END); 
    } 

    // Ignore this code. It simply is present to create image icons 
    // without having to use an actual image. This way you can run this code without an image 
    private Icon createMyIcon(Color color) { 
     BufferedImage img = new BufferedImage(BI_WIDTH, BI_WIDTH, BufferedImage.TYPE_INT_ARGB); 
     Graphics2D g2 = img.createGraphics(); 
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     g2.setColor(color); 
     g2.fillOval(5, 5, BI_WIDTH - 10, BI_WIDTH - 10); 
     g2.setStroke(new BasicStroke(10f)); 
     g2.setColor(Color.LIGHT_GRAY); 
     g2.drawOval(5, 5, BI_WIDTH - 10, BI_WIDTH - 10); 
     g2.dispose(); 
     return new ImageIcon(img); 
    } 

    private class TimerListener implements ActionListener { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      // get icon from button 
      Icon icon = greenButton.getIcon(); 

      // check if it's the green icon 
      if (icon == greenIcon) { 
       icon = darkGreenIcon; // if so, make it the dark green icon 
      } else { 
       icon = greenIcon; // if not, make it the green icon 
      } 
      greenButton.setIcon(icon); // stuff it back into the button 
     } 
    } 

    // this is my startButton's Action. 
    // an Action is like an "ActionListener on steroids" 
    private class StartAction extends AbstractAction { 
     public StartAction(String name) { 
      super(name); // the text that appears in the button 
      putValue(MNEMONIC_KEY, (int) name.charAt(0)); // the alt-key mnemonic 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      if (timer.isRunning()) { // if the timer is currently running 
       timer.stop(); // stop the Timer 
       greenButton.setIcon(greenIcon); // set the icon back to the defaut green icon 
       putValue(NAME, START); // change the button's text to "Start" 
      } else { // otherwise the Timer's not running 
       timer.start(); // Start it 
       putValue(NAME, STOP); // change this button's text to "Stop" 
      } 
     } 
    } 

    private static void createAndShowGui() { 
     JFrame frame = new JFrame("Flashing Button"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(new FlashingButton()); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> createAndShowGui()); 
    } 
}