Flashing JTextField and JButton

徘徊边缘 提交于 2019-12-13 05:13:44

问题


//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);

I am trying to create two textfields where the users can type in what they want and a button to save their input to a variable. But when I run my program, the text fields and button flash and then become invisible. Then if the user clicks on them they flash again. How do I stop this from happening?

The rest of the class:

// 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);

}

回答1:


Looks like while(running) and Thread.sleep is giving your problems. For Swing apps use a javax.swing.Timer. Here is the basic construct

Timer(int delay, ActionListener listener)

where delay is the time to be delayed ("slept") between intervals and the listener is used just like a listener for a button, except you don't need to press a button for the event to fire, the Timer fires the event every delayed milliseconds. You can do something like this

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

Here's an example of a Timer being used. Take a look at the docs for more methods you can use with the timer.


Side Note

It's not recommended to use a null layout. Take a look at Laying out Components within a Container for details on how to use Layout Managers, as you should be using instead of a null layout.



来源:https://stackoverflow.com/questions/21516220/flashing-jtextfield-and-jbutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!