按钮未出现在循环中

时间:2014-01-19 22:59:26

标签: java loops applet

我在制作Java Applet时遇到问题,我试图让 3个单独的按钮出现在屏幕上,然后在点击时执行操作。我创建了一个for循环,并在里面添加了按钮,但是当我运行程序时,没有按钮出现。有什么建议?

public class CPT extends Applet
{
    Button Holland, Cuba, Russia, USACal, Austria, Colombia, China, SouthKor, Bangladesh, DomRep;
    int ranDeal;
    Label title;
    Font f;
    Image globe, signs, plane;

public void init ()
    {
        Button Holland = new Button ("Holland");
        Button Cuba = new Button ("Cuba");
        Button Russia = new Button ("Russia");
        Button USACal = new Button ("USA - Cali.");
        Button Austria = new Button ("Austria");
        Button Colombia = new Button ("Colombia");
        Button China = new Button ("China");
        Button SouthKor = new Button ("South Korea");
        Button Bangladesh = new Button ("Bangladesh");
        Button DomRep = new Button ("Dom. Rep.");

    for (int j = 0 ; j >= 4 ; j++)
    {

        ranDeal = (int) (Math.random () * 10);

        if (ranDeal == 1)
        {

            add (Holland);
        }
        else if (ranDeal == 2)
        {

            add (Cuba);
        }
        else if (ranDeal == 3)
        {

            add (Russia);
        }
        else if (ranDeal == 4)
        {

            add (USACal);
        }
        else if (ranDeal == 5)
        {

            add (Austria);
        }
        else if (ranDeal == 6)
        {

            add (Colombia);
        }
        else if (ranDeal == 7)
        {

            add (China);
        }

        else if (ranDeal == 8)
        {

            add (SouthKor);
        }
        else if (ranDeal == 9)
        {

            add (Bangladesh);
        }
        else
        {

            add (DomRep);



        }

    }

1 个答案:

答案 0 :(得分:3)

你永远不会进入这个循环。

for (int j = 0 ; j >= 4 ; j++)

我想你的意思。

for (int j = 0 ; j < 4 ; j++)

此外,for循环中的逻辑存在此问题,即它可以多次选择相同的按钮,并分别尝试多次添加。