Javax.swing定时器重复正常,但ActionListener不做任何事情

我想在文本字段中闪烁背景颜色。 我的计时器设置如下:

Flash flash = new Flash(); //set up timer tmr = new javax.swing.Timer(1000, new Flash()); tmr.addActionListener(flash); tmr.setInitialDelay(0); tmr.setRepeats(true); tmr.start(); 

我的actionListener如下所示:

  static class Flash implements ActionListener { public void actionPerformed(ActionEvent evt) { if (flasher) { SpreademPanel.historyPnl.NameTxt.setBackground(Color.white); } else { SpreademPanel.historyPnl.NameTxt.setBackground(Color.pink); } flasher = !flasher; } //actionPerformed } //Flash 

现在,当我把这个进行debugging并且遵循这个动作的时候,程序会重复地通过闪存并且在这两个select之间切换。 但在屏幕上,只有第一个切换发生。 之后,尽pipe闪光仍在运作,但没有任何行动。

这里有什么问题?

在此先感谢您的帮助。

这里有几个问题。

第一个显而易见的事情是,你似乎在使用可变静态。 这是一个非常糟糕的主意,并指出(并导致!)混乱。 在这种特殊情况下,引起的问题之一是flasher静电是共享的。

 Flash flash = new Flash(); //set up timer tmr = new javax.swing.Timer(1000, new Flash()); tmr.addActionListener(flash); 

我们正在添加两个Flash动作。 通常情况下这将是不好的,但只是产生一个无法检测的“错误”。 颜色将被设置两次。

把这两件事情结合在一起,我们有两个没有中断的行为来执行相同的切换。 两个切换。 国家没有改变(虽然有重绘,财产变化事件等)。

所以,不要使用可变静态,并保持代码清洁。

这个例子不断地改变面板的背景颜色的饱和度 :

FlashTest

 import java.awt.*; import java.awt.event.*; import java.awt.event.ActionListener; import java.util.LinkedList; import java.util.Queue; import javax.swing.*; public class FlashTest extends JPanel { private static final Font font = new Font("Serif", Font.PLAIN, 32); private static final String s = "Godzilla alert!"; FlashTest() { this.setPreferredSize(new Dimension(256, 96)); this.setBackground(Color.red); Timer t = new Timer(50, new Flash(this)); t.start(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setFont(font); int xx = this.getWidth(); int yy = this.getHeight(); int w2 = g.getFontMetrics().stringWidth(s) / 2; int h2 = g.getFontMetrics().getDescent(); g.setColor(Color.black); g.drawString(s, xx / 2 - w2, yy / 2 + h2); } private static class Flash implements ActionListener { private final float N = 32; private final JComponent component; private final Queue<Color> clut = new LinkedList<Color>(); public Flash(JComponent component) { this.component = component; for (int i = 0; i < N; i++) { clut.add(Color.getHSBColor(1, 1 - (i / N), 1)); } for (int i = 0; i < N; i++) { clut.add(Color.getHSBColor(1, i / N, 1)); } } @Override public void actionPerformed(ActionEvent e) { component.setBackground(clut.peek()); clut.add(clut.remove()); } } static public void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new FlashTest()); f.pack(); f.setVisible(true); } }); } } 
 tmr = new javax.swing.Timer(1000, flash); 

我试过你的代码,它工作正常。

为什么使用SpreademPanel.historyPnl.NameTxt的静态上下文?

编辑

您可能需要重新devise您的类以在构造函数中传递该组件。

 private class Flash implements ActionListener { private boolean flasher = false; private JComponent component; public Flash(JComponent component) { this.component = component; } public void actionPerformed(ActionEvent evt) { if (flasher) { component.setBackground(Color.white); } else { component.setBackground(Color.pink); } flasher = !flasher; } //actionPerformed } //Flash 

然后用它初始化

  Flash flash = new Flash(SpreademPanel.historyPnl.NameTxt); Timer tmr = new javax.swing.Timer(1000, flash); tmr.start();