除非我在运行屏幕时移动屏幕,否则绘制汽车或卡车的循环不断卡住

时间:2019-04-26 18:03:49

标签: java swing

public class vehicleViewer {

    public static void main(String args[]) throws InterruptedException{


        JFrame f = new JFrame();
        vehicleComponent c = new carComponent();
        vehicleComponent t = new truckComponent();

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(400, 400);

        c.seats();
        c.colors();
        t.seats();
        t.colors();

        System.out.println("Randomly generating...");

        ////////////////////////////////////////// random number gen
        Random dice = new Random();
        int number;

        for(int counter=1; counter<=20;counter++){
            number = dice.nextInt(2);
            Thread.sleep(1000);
            if (number == 0){ //if show 0, draw a car
                System.out.print("Car \n");
                f.add(c); //show the car
                f.remove(t); //remove the truck

            }
            else if (number == 1){ //if roll 1, draw a truck
                System.out.print("Truck \n");
                f.add(t); //show the truck
                f.remove(c); //remove the car

            }
            f.setVisible(true);
            Thread.sleep(1000); 


        }

    }
}

我有一个随机生成器,如果它生成0或1,它将在JFrame上绘制一辆汽车(0)或一辆卡车(1)。第一次从一个交换到另一个后,它将不再交换,只会停留在第二辆车上。唯一会更新的是运行时是否手动更改屏幕尺寸。

0 个答案:

没有答案