Javax.swing定时器重复正常,但ActionListener不执行任何操作 [英] Javax.swing timer repeats fine, but ActionListener doesn't do anything

查看:255
本文介绍了Javax.swing定时器重复正常,但ActionListener不执行任何操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  Flash flash = new Flash(); //设置计时器
tmr = new javax.swing.Timer(1000,new Flash());
tmr.addActionListener(flash);
tmr.setInitialDelay(0);
tmr.setRepeats(true);
tmr.start();

我的actionListener如下:

 静态类Flash实现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

现在,当我把这在调试中并遵循动作,程序会反复执行闪存并在两个备选方案之间切换。但是在屏幕上,只有第一个切换
出现。之后,没有动作,虽然闪光灯仍在运作。



这里有什么问题?



提前致谢任何帮助。

解决方案

这里有一些问题。



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

  Flash flash = new Flash(); //设置计时器
tmr = new javax.swing.Timer(1000,new Flash());
tmr.addActionListener(flash);

我们正在添加两个 Flash 操作。通常这会很糟糕,但只会产生一个无法察觉的错误。颜色将被设置两次。



将这两个东西放在一起,我们有两个没有中断的动作执行相同的切换。两个切换。状态不会改变(虽然有重新绘制,属性更改事件等)。



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

I am trying to flash the background colour in a textfield. My timer setup is as follows:

 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();                 

My actionListener is as follows:

 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

Now, when I put this in debug and follow the action, the program does repeatedly step through flash and toggle between the two alternatives. But onscreen, only the first toggle occurs. After that, no action, although flash is still functioning.

What is wrong here?

Thanks in advance for any help.

解决方案

There are a couple of problems here.

The first obvious thing is that you appear to be using mutable statics. This is a really bad idea and indicates (and causes!) confusion. In this particular case, one of the problems caused is that the flasher static is shared.

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

We are adding two Flash actions. Ordinarily this would be bad, but just produce an undetectable "bug". The colour would be set twice.

Bring these two things together, and we have two actions without a break that perform the same toggle. Two toggles. The state does not change (although there are repaint, property change events, etc.).

So, don't use mutable statics, and keep the code clean.

这篇关于Javax.swing定时器重复正常,但ActionListener不执行任何操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆