Javax.swing 计时器重复正常,但 ActionListener 不执行任何操作

标签 java swing javax.swing.timer

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

 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

现在,当我将其置于调试状态并执行操作时,程序确实会重复单步执行 flash 并在两个备选方案之间切换。但在屏幕上,只有第一个切换 发生。之后,没有任何 Action ,尽管 flash 仍在运行。

这里有什么问题吗?

在此先感谢您的帮助。

最佳答案

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

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

关于Javax.swing 计时器重复正常,但 ActionListener 不执行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2123841/

相关文章:

java - 如何从模式 dd-MMM-yyyy 中的给定字符串日期获取整数中的日月和年

java - 为什么矩形跳跃的高度会变化?

java - 启用停止定时器

java - 带有 Thread.sleep 的 DocumentListener

java - 如果没有更新则进行 Ant 清理

java - android fragment 使用 onCreateView 但无法添加 firebase

java - 在 auth 库中找不到类 GoogleSignIn

java - JComboBox 的值

java - 区分大小写的 jcombobox