Java gui按钮的延迟

Java gui按钮的延迟,java,swing,jbutton,delay,Java,Swing,Jbutton,Delay,在一点点代码上有一点小问题。我想使用一个延迟,这样用户在点击正确的按钮后可以在有限的时间内看到gui,然后它会重置回原来的形式。我试图使用thread.sleep()代码来执行此操作,但编译器中不断出现异常错误。我想也许一个解决办法是使用摆动计时器,但不知道这是否可能。如果您对正确的行动方案有任何反馈,我们将不胜感激 import java.awt.*; import javax.swing.*; import java.util.*; import java.awt.event.*;

在一点点代码上有一点小问题。我想使用一个延迟,这样用户在点击正确的按钮后可以在有限的时间内看到gui,然后它会重置回原来的形式。我试图使用thread.sleep()代码来执行此操作,但编译器中不断出现异常错误。我想也许一个解决办法是使用摆动计时器,但不知道这是否可能。如果您对正确的行动方案有任何反馈,我们将不胜感激

 import java.awt.*;
 import javax.swing.*;
 import java.util.*;
 import  java.awt.event.*;
 import java.lang.Thread;




public class Test extends JFrame implements ActionListener  {

JLabel label1, label2, label3;


 ImageIcon image1, image2, image3, image4, image5; 

 JTextField textResult; 

  JButton [] buttons; 

 int treasureLocation; 

 int clicks = 0;



 Thread td = new Thread(); //Declare thread    




  public static void main(String[] args) {




  new Test();

  }


 public Test (){

  this.setSize(700,700);
  this.setLocationRelativeTo(null);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setTitle("Treasure Hunt Game");

  JPanel thePanel = new JPanel();


  thePanel.setLayout(new GridLayout(0,3,0,0));

  image1 = new ImageIcon(getClass().getResource("Treasure.jpg"));
  image2 = new ImageIcon(getClass().getResource("Pirate.jpg"));
  image3 = new ImageIcon(getClass().getResource("sand2.jpg"));
  image4 = new ImageIcon(getClass().getResource("emptyhole.jpg"));   
  image5 = new ImageIcon(getClass().getResource("map.jpg"));

  label1 = new JLabel("Click the buttons!");
  label1.setFont(new Font("Serif", Font.PLAIN, 20));
  label1.setForeground(Color.red);


  label2 = new JLabel(image5); 
  label3 = new JLabel(image2);


  buttons = new JButton[9]; 
  buttons[0] = new JButton(image3);
  buttons[1] = new JButton(image3);
  buttons[2] = new JButton(image3);
  buttons[3] = new JButton(image3);
  buttons[4] = new JButton(image3);
  buttons[5] = new JButton(image3);
  buttons[6] = new JButton(image3);
  buttons[7] = new JButton(image3);
  buttons[8] = new JButton(image3);





  thePanel.add(buttons[0]);
  thePanel.add(buttons[1]);
  thePanel.add(buttons[2]);
  thePanel.add(buttons[3]);
  thePanel.add(buttons[4]);
  thePanel.add(buttons[5]);
  thePanel.add(buttons[6]);
  thePanel.add(buttons[7]);
  thePanel.add(buttons[8]);
  thePanel.add(label1); 
  thePanel.add(label2);
  thePanel.add(label3);

  buttons[0].addActionListener(this);
  buttons[1].addActionListener(this);
  buttons[2].addActionListener(this);
  buttons[3].addActionListener(this);
  buttons[4].addActionListener(this);
  buttons[5].addActionListener(this);
  buttons[6].addActionListener(this);
  buttons[7].addActionListener(this);
  buttons[8].addActionListener(this);



  this.add(thePanel);

  this.setVisible(true);



  treasureLocation = new Random().nextInt(buttons.length);


  System.out.println(treasureLocation);




  }




  public void actionPerformed(ActionEvent evt){
   Object source = evt.getSource();

   if (source == buttons[treasureLocation]) {


     buttons[treasureLocation].setIcon(image1);
     label1.setText("You've found me Treasure!");

     td.sleep(3000); //Delay and reset code
     Test.this.setVisible(false);
     Test.this.dispose();
     new Test();



    }
   else 
   { 
     ((JButton)source).setIcon(image4); 

  }

  clicks++;
  System.out.println(clicks);
  if (clicks == 5){

     label1.setText("One more try Matey!");

  }

  if ((clicks == 6) && (source !=  buttons[treasureLocation])) {

     label1.setText("Game over!");

  }
  if (clicks == 7){

     Test.this.setVisible(false);
     Test.this.dispose();
     new Test();
  }

  }





   }

线程睡眠将使整个GUI进入睡眠状态,包括绘制,因此永远不要在Swing事件线程上使用此选项。改用摆动计时器

我还建议不要显示新的JFrame或GUI,因为向用户抛出多个窗口可能会让人恼火

e、 g


有关详细信息,请参阅。

线程睡眠将使整个GUI进入睡眠状态,包括绘制,因此切勿在Swing事件线程上使用此选项。改用摆动计时器

我还建议不要显示新的JFrame或GUI,因为向用户抛出多个窗口可能会让人恼火

e、 g

有关详细信息,请参阅

public void actionPerformed(ActionEvent e) {
  final Object source = evt.getSource();

  if (source == buttons[treasureLocation]) {
     buttons[treasureLocation].setIcon(image1);
     label1.setText("You've found me Treasure!");

     Timer timer = new Timer(TIMER_DELAY, new ActionListener() {
       public void actionPerformed(ActionEvent evt) {
         // reset GUI including the JButton's icon
       }
     });
     timer.setRepeats(false);
     timer.start();
  }  
}