简体   繁体   English

Java,有关线程/摆动的帮助

[英]Java, help with threading/swing

import java.awt.Color;


public class bullet {

    public bullet(int x, int y, boolean up)
    {
        System.out.println("Called");    
        int start = up?15-y:Math.abs(y-15);
        int cNt = 0;
        System.out.println("Start - " + start);

        for(int i=start;15>start;start++)
        {
            try {
                System.out.println("Its red");
                engineMenu.staticSGC(x,cNt,Color.RED);
                Thread.sleep(300);
            } catch (InterruptedException e) {}

            System.out.println("White - " + i + "," + cNt); 
            engineMenu.staticSGC(x,cNt,Color.WHITE);
            cNt += 1;
        }
    }

}

All engineMenu.staticSGC does is change the color of a JPanel- it works fine. engineMenu.staticSGC所做的全部工作就是更改JPanel的颜色-效果很好。

What is happening is the print statements run and after they all run it makes a line of white JPanels, instead of setting them to red then White when they are supposed to. 发生的事情是运行print语句,并且在它们全部运行之后,它会生成一行白色的JPanels,而不是在应有的情况下将它们设置为红色,然后再设置为White。 Any clue whats wrong? 任何线索出什么事了吗?

I would guess you are executing your code on the EDT and the sleep() method is preventing the GUI from repainting itself. 我猜想您正在EDT上执行代码,而sleep()方法阻止了GUI重新绘制自身。

Read up on Concurrency in Swing for more information and solutions. 阅读Swing中的并发以获取更多信息和解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM