您似乎試圖在自己的偵聽器中重新啓動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());
}
}
是,使用一個Swing計時器,沒有,不換按鈕。交換圖標顯示的按鈕 - ***很多***更簡單和白癡證明。所有你需要做的就是調用定時器內的按鈕上的'setIcon(...)'。 –
[不使用JButton的Simon遊戲的另一個例子,但可能有用](http://stackoverflow.com/questions/33027235/how-to-make-keypress-work-with-keylistener)我。 –
@HovercraftFullOfEels謝謝生病嘗試使用它,但我的主要問題是使用計時器,如果你可以提供任何煽動 – McD