简体   繁体   中英

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.

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. 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.

Read up on Concurrency in Swing for more information and solutions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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