gpt4 book ai didi

java - 闪烁 JTextField 和 JButton

转载 作者:行者123 更新时间:2023-12-02 06:15:32 25 4
gpt4 key购买 nike

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

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

类(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);

}

最佳答案

看起来 while(running)Thread.sleep 给你带来了问题。对于 Swing 应用程序,请使用 javax.swing.Timer。这是基本结构

Timer(int delay, ActionListener listener)

其中 delay 是间隔之间要延迟的时间(“ sleep ”),并且 listener 的使用就像一个监听器按钮,除非您不需要按下按钮来触发事件,Timer 每隔delayed 毫秒触发事件。你可以做这样的事情

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

这是一个example正在使用的Timer。看看docs了解更多可以与计时器一起使用的方法。

<小时/>

旁注

不建议使用空布局。看看Laying out Components within a Container有关如何使用布局管理器的详细信息,因为您应该使用布局管理器而不是空布局。

关于java - 闪烁 JTextField 和 JButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21516220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com