我有一个继承自JPanel的类,上面有一个图像,我想设置一个小动画来显示面板/图像,然后在事件触发时将其淡出.
我可能会设置一个线程并触发动画,但我该怎么做呢?
我试图在文本字段中闪烁背景颜色.我的计时器设置如下:
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();
Run Code Online (Sandbox Code Playgroud)
我的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
Run Code Online (Sandbox Code Playgroud)
现在,当我把它放在调试中并按照动作执行时,程序会反复执行闪存并在两个备选项之间切换.但是在屏幕上,只有第一个切换发生.之后,没有动作,虽然闪光灯仍在运转.
这有什么不对?
在此先感谢您的帮助.