Java JButton的颜色转换

Java JButton的颜色转换,java,swing,jbutton,Java,Swing,Jbutton,我正在尝试使一个JButton经历颜色转换,因此我向它添加了一个动作侦听器,它执行以下操作: public void actionPerformed(ActionEvent arg0) { Color c = new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256)); Color cc = v.get(1).getBackground(); //(...) T

我正在尝试使一个
JButton
经历颜色转换,因此我向它添加了一个动作侦听器,它执行以下操作:

public void actionPerformed(ActionEvent arg0) {
    Color c = new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256));
    Color cc = v.get(1).getBackground();
    //(...) The r, rr, b, bb, g, gg are just the components of the colors c and cc
    boolean boo = true;
    while(boo){
        if(r < rr){
            r++;
        }
        if(b < bb){
            b++;
        }
        if(g < gg){
            g++;
        }
        if(r == rr && b == bb && g == gg){
            boo = false;
        }
        Color color = new Color(r, b, g);
        v.get(1).setBackground(color);
        v.get(0).setBackground(color);
        frame.repaint();
        frame.revalidate();
    }
public void actionPerformed(ActionEvent arg0){
颜色c=新颜色((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256));
Color cc=v.get(1.getBackground();
//(…)r、rr、b、bb、g、gg只是颜色c和cc的组成部分
布尔boo=true;
while(boo){
if(r

但这并不能实现转换,它实际上只是改变了按钮的颜色。我这里缺少什么?

您需要使用另一个线程来执行此逻辑,并在操作发生时启动该线程。长操作会阻止负责图形更改的线程

因此,您的问题是,您希望实时完成每个图形更改,但不是与单独的线程异步工作,而是同步工作,在事件函数完成时释放EDT。

“我缺少什么?”不要阻止EDT(事件调度线程)。发生这种情况时,GUI将“冻结”。有关详细信息和修复方法,请参阅(计时器)。