//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);
我正在尝试创建两个Textfield,用户可以在其中输入他们想要的内容和一个按钮以将输入保存到变量。但是,当我运行程序时,文本字段和按钮闪烁,然后变得不可见。然后,如果用户单击它们,他们再次闪烁。我如何阻止这种情况发生?
班级的其余部分:
// 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
正在给您问题。对于摇摆应用,请使用javax.swing.Timer
。这是基本的构造
Timer(int delay, ActionListener listener)
delay
是间隔和 listener
之间延迟延迟的时间(" slept" ),就像按钮的侦听器一样,除了您不需要按下按钮要开火的事件,Timer
发射了每个delay
ED毫秒。你可以做这样的事情
Timer timer = new Timer(delay, new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// the stuff in your while loop here
}
});
timer.starts();
这是使用Timer
的示例。查看 docs 有关可以与计时器一起使用的更多方法。
旁注
不建议使用空布局。请查看在容器中布置组件,以获取有关如何使用布局管理器的详细信息,因为您应该使用的那样而不是空布局。