Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 闪烁JTextField和JButton_Java_Swing_Jbutton_Jtextfield_Null Layout Manager - Fatal编程技术网

Java 闪烁JTextField和JButton

Java 闪烁JTextField和JButton,java,swing,jbutton,jtextfield,null-layout-manager,Java,Swing,Jbutton,Jtextfield,Null Layout Manager,我试图创建两个文本字段,用户可以在其中键入他们想要的内容,并创建一个按钮将输入保存到变量中。但当我运行程序时,文本字段和按钮会闪烁,然后变得不可见。然后,如果用户单击它们,它们会再次闪烁。我该如何阻止这种情况发生 班上其他同学: //ADDING BET FIELDS setLayout(null); horseChoice = new JTextField(); horseChoice.setBounds(200, 585, 300, 20); add(horseChoice); setL

我试图创建两个文本字段,用户可以在其中键入他们想要的内容,并创建一个按钮将输入保存到变量中。但当我运行程序时,文本字段和按钮会闪烁,然后变得不可见。然后,如果用户单击它们,它们会再次闪烁。我该如何阻止这种情况发生

班上其他同学:

//ADDING BET FIELDS
setLayout(null); 
horseChoice = new JTextField();
horseChoice.setBounds(200, 585, 300, 20);
add(horseChoice);

setLayout(null); 
horseBet = new JTextField();
horseBet.setBounds(200, 625, 300, 20);
add(horseBet);

//ADDING BET BUTTON
setLayout(null); 
bet = new JButton("Place Bet");
bet.setBounds(250, 675, 200, 50);
add(bet);
//主函数
公开募捐{
init();
字体=新字体(“Arial”,font.BOLD,14);
titleFont=新字体(“Comic Sans MS”,Font.BOLD,20);
textColor=新颜色(100100100);
结果颜色=新颜色(255、165、0、180);
//添马
horse1=新马_1();
horse2=新马_2();
horse3=新马_3();
horse4=新马_4();
//游戏圈
(跑步时){
start=System.nanoTime();
已用=System.nanoTime()-开始;
等待=targetTime-已用时间/1000000;
试一试{
线程。睡眠(等待);
}
捕获(例外e){
e、 printStackTrace();
}
//计时器
如果(马力1.h1x<1350){
horse1Time++;
}
如果(马力2.h2x<1350){
horse2Time++;
}
如果(马力3.h3x<1350){
horse3Time++;
}
如果(马力4.h4x<1350){
horse4Time++;
}
//停止计时
如果(horse1Result==true&&horse1.h1x>=1350){
horse1TimeStr=horse1Time+“,多米尼克”;
horse1Result=false;
}
如果(horse2Result==true&&horse2.h2x>=1350){
horse2TimeStr=horse2Time+“,Charlie”;
horse2Result=false;
}
如果(horse3Result==true&&horse3.h3x>=1350){
horse3TimeStr=horse3Time+“,上将”;
horse3Result=false;
}
如果(horse4Result==true&&horse4.h4x>=1350){
horse4TimeStr=horse4Time+“,百加得”;
horse4Result=false;
}
//随机选马和移动位置
如果(horse1.h1x>=1350){
最终int[]选项={2,3,4};
}
否则如果(horse2.h2x>=1350){
最终int[]选项={1,3,4};
}
否则如果(horse3.h3x>=1350){
最终int[]选项={1,2,4};
}
否则如果(马力4.h4x>=1350){
最终int[]选项={1,2,3};
}
否则{
最终int[]选项={1,2,3,4};
}
randomHorse=CHOICES[rand.nextInt(CHOICES.length)];
randomMove=rand.nextInt(35)+1;
//地点
更新();
Draw();
drawToScreen();
}
}
//更新游戏
私有void更新(){
//更新竞赛计数器
如果(horse1.h1x==100&&horse2.h2x==100&&horse3.h3x==100&&horse4.h4x==100){
种族++;
}
//马匹更新
horse1.Update();
horse2.Update();
horse3.Update();
horse4.Update();
//结果设置
if(result==true&&horse1Result==false&&horse2Result==false&&horse3Result==false&&horse4Result==false){
串[]匹马=
{horse1TimeStr,horse2TimeStr,horse3TimeStr,horse4TimeStr};
数组。排序(马);
第一匹马=马[0];
第二匹马=马[1];
第三匹=马[2];
第四匹马=马[3];
firstHorse=firstHorse.replaceAll([0-9,,],”);
secondHorse=secondHorse.replaceAll(“[0-9,,]”,”);
thirdHorse=thirdHorse.replaceAll(“[0-9,,]”,”);
fourthHorse=fourthHorse.replaceAll(“[0-9,,]”,”);
结果=假;
}
}
//将游戏绘制到屏幕外缓冲图像上
私有void Draw(){
//背景色
g、 setColor(Color.WHITE);
g、 fillRect(0,0,宽度,高度);
如果(按钮==真){
//添加下注字段
setLayout(空);
horschoice=newjtextfield();
马蹄铁。挫折(2005853000,20);
添加(horseChoice);
setLayout(空);
horseBet=新的JTextField();
马比.倒退(200625300,20);
加(马匹);
//添加下注按钮
setLayout(空);
下注=新的JButton(“下注”);
赌注.挫折(250675200,50);
加(赌);
按钮=假;
}
1.牵引力(g);
2.牵引力(g);
3.牵引力(g);
4.抽签(g);
//马道1号
g、 setColor(新颜色(255,0,0));
g、 抽绳(100125,宽度-100125);
//马道2号
g、 setColor(新颜色(0,255,0));
g、 抽绳(100225,宽度-100225);
//马道3号
g、 setColor(新颜色(0,0,255));
g、 抽绳(100325,宽度-100325);
//马道4号
g、 setColor(新颜色(255,255,0));
g、 抽绳(100425,宽度-100425);
//终点线
g、 setColor(新颜色(255,255,0));
g、 抽绳(宽度-100475,宽度-100375);
g、 setColor(新颜色(0,0,255));
G
// the "main" function
public void run(){

    init();



            font = new Font ("Arial", Font.BOLD, 14);
            titleFont = new Font ("Comic Sans MS", Font.BOLD, 20);
    textColor = new Color(100, 100, 100);
            resultColor = new Color(255, 165, 0, 180);

            //ADDING HORSES
            horse1 = new Horses_1();
            horse2 = new Horses_2();
            horse3 = new Horses_3();
            horse4 = new Horses_4();



    //GAMELOOP
    while (running){

        start = System.nanoTime();

        elapsed = System.nanoTime() - start;

        wait = targetTime - elapsed / 1000000;

        try{

            Thread.sleep(wait);

        }
        catch(Exception e){
            e.printStackTrace();
        }

                    //TIMER
                    if(horse1.h1x < 1350){
                        horse1Time++;
                    }
                    if(horse2.h2x < 1350){
                        horse2Time++;
                    }
                    if(horse3.h3x < 1350){
                        horse3Time++;
                    }
                    if(horse4.h4x < 1350){
                        horse4Time++;
                    }

                    //STOP RACING TIMERS
                    if(horse1Result == true && horse1.h1x >= 1350){
                        horse1TimeStr = horse1Time + ", Dominic";
                        horse1Result = false;
                    }
                    if(horse2Result == true && horse2.h2x >= 1350){
                        horse2TimeStr = horse2Time + ", Charlie";
                        horse2Result = false;
                    }
                    if(horse3Result == true && horse3.h3x >= 1350){
                        horse3TimeStr = horse3Time + ", Admiral";
                        horse3Result = false;
                    }
                    if(horse4Result == true && horse4.h4x >= 1350){
                        horse4TimeStr = horse4Time + ", Bacardi";
                        horse4Result = false;
                    }


                    //RANDOM HORSE CHOICE & MOVE PLACES
                    if(horse1.h1x >= 1350){
                        final int[] CHOICES = { 2,3,4 };

                    }
                    else if(horse2.h2x >= 1350){
                        final int[] CHOICES = { 1,3,4 };
                    }
                    else if(horse3.h3x >= 1350){
                        final int[] CHOICES = { 1,2,4 };
                    }
                    else if(horse4.h4x >= 1350){
                        final int[] CHOICES = { 1,2,3 };
                    }
                    else{
                        final int[] CHOICES = { 1,2,3,4 };
                    }

                    randomHorse = CHOICES[rand.nextInt(CHOICES.length)];
                    randomMove = rand.nextInt(35) + 1;

                    //PLACES
        Update();
        Draw();
        drawToScreen();
    }

}




// updates the game
private void Update() {


        //UPDATE RACE COUNTER
        if(horse1.h1x == 100 && horse2.h2x == 100 && horse3.h3x == 100 && horse4.h4x == 100){
            races++;
        }

        //HORSE UPDATE


        horse1.Update();
        horse2.Update();
        horse3.Update();
        horse4.Update();

        //RESULT SETTING
        if(result == true && horse1Result == false && horse2Result == false && horse3Result == false && horse4Result == false){


            String[] horses =
                { horse1TimeStr, horse2TimeStr, horse3TimeStr, horse4TimeStr };

            Arrays.sort(horses);
            firstHorse = horses[0];
            secondHorse = horses[1];
            thirdHorse = horses[2];
            fourthHorse = horses[3];

            firstHorse = firstHorse.replaceAll("[0-9,,]","");
            secondHorse = secondHorse.replaceAll("[0-9,,]","");
            thirdHorse = thirdHorse.replaceAll("[0-9,,]","");
            fourthHorse = fourthHorse.replaceAll("[0-9,,]","");

            result = false;

        }





}

// draws the game onto an off-screen buffered image
private void Draw() {   

            //BACKGROUND COLOUR
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, WIDTH, HEIGHT);

            if(button == true){
                //ADDING BET FIELDS
                setLayout(null); 
                horseChoice = new JTextField();
                horseChoice.setBounds(200, 585, 300, 20);
                add(horseChoice);

                setLayout(null); 
                horseBet = new JTextField();
                horseBet.setBounds(200, 625, 300, 20);
                add(horseBet);

                //ADDING BET BUTTON
                setLayout(null); 
                bet = new JButton("Place Bet");
                bet.setBounds(250, 675, 200, 50);
                add(bet);

                button = false;
            }


            horse1.Draw(g);
            horse2.Draw(g);
            horse3.Draw(g);
            horse4.Draw(g);

            //HORSE 1 - TRACK
            g.setColor(new Color (255, 0, 0));
            g.drawLine(100, 125, WIDTH - 100, 125);

            //HORSE 2 - TRACK
            g.setColor(new Color (0, 255, 0));
            g.drawLine(100, 225, WIDTH - 100, 225);

            //HORSE 3 - TRACK
            g.setColor(new Color (0, 0, 255));
            g.drawLine(100, 325, WIDTH - 100, 325);

            //HORSE 4 - TRACK
            g.setColor(new Color (255, 255, 0));
            g.drawLine(100, 425, WIDTH - 100, 425);  

            //FINISH LINE
            g.setColor(new Color (255, 255, 0));
            g.drawLine(WIDTH - 100, 475, WIDTH - 100, 375);

            g.setColor(new Color (0, 0, 255));
            g.drawLine(WIDTH - 100, 275, WIDTH - 100, 375);

            g.setColor(new Color (0, 255, 0));
            g.drawLine(WIDTH - 100, 175, WIDTH - 100, 275);

            g.setColor(new Color (255, 0, 0));
            g.drawLine(WIDTH - 100, 75, WIDTH - 100, 175);

            //HUD
            g.setColor(new Color (180, 180, 180));
            g.drawLine(50, 550, WIDTH - 50, 550);
            g.drawLine(550, 550, 550, 700);
            g.drawLine(900, 550, 900, 700);

            g.setColor(resultColor);
            g.setFont(titleFont);
            g.drawString("Results", 700, 540);
            g.drawString("Details", 1000, 540);
            g.drawString("Betting", 360, 540);

            //Results
            g.setColor(textColor);
    g.setFont(font);
            g.drawString("Winner : ", 600, 600);
            g.drawString("Second : ", 600, 630);
            g.drawString("Third : ", 600, 660);
            g.drawString("Fourth : ", 600, 690);

            //DETAILS
            g.drawString("Race Number :", 950, 600);
            g.drawString("Horse Choice :", 950, 630);
            g.drawString("Bet :", 950, 660);

            g.setColor(resultColor);
            g.drawString("" + races, 1250, 600);

            //BETS
            g.setColor(textColor);
            g.drawString("Horse Choice :", 75, 600);
            g.drawString("Bet :", 75, 640);


            if(horse1Result == false && horse2Result == false && horse3Result == false && horse4Result == false){
                g.setColor(resultColor);
                g.drawString(" " + firstHorse, 780, 600);
                g.drawString(" " + secondHorse, 780, 630);
                g.drawString(" " + thirdHorse, 780, 660);
                g.drawString(" " + fourthHorse, 780, 690);
            }

            g.setColor(textColor);
    g.setFont(font);

            //HORSE 1 TEXT
    String h1 = "Dominic -";
    g.drawString(h1, 10, 130);

            //HORSE 2 TEXT
            String h2 = "Charlie -";
    g.drawString(h2, 10, 230);

            //HORSE 3 TEXT
            String h3 = "Admiral -";
    g.drawString(h3, 10, 330);

            //HORSE 4 TEXT
            String h4 = "Bacardi -";
    g.drawString(h4, 10, 430);

}
Timer(int delay, ActionListener listener)
Timer timer = new Timer(delay, new ActionListener(){
    @Override 
    public void actionPerformed(ActionEvent e) {
        // the stuff in your while loop here
    }
});
timer.starts();