How to create a delay in Swing

后端 未结 4 759
长情又很酷
长情又很酷 2020-11-30 14:49

I made a blackjack game, and I want the AI player to pause between taking cards. I tried simply using Thread.sleep(x), but that makes it freeze until the AI player is done t

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 15:34

    Well, the following code shows a JFrame with a JTextArea and a JButton. When the buttons is clicked, the Timer send the event repeatedly (with a second delay between them) to the actionListener related to the button which appends a line with the current time.

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Calendar;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.Timer;
    
    
    public class TimerTest extends JFrame implements ActionListener{
    
        private static final long serialVersionUID = 7416567620110237028L;
        JTextArea area;
        Timer timer;
        int count; // Counts the number of sendings done by the timer
        boolean running; // Indicates if the timer is started (true) or stopped (false)
    
        public TimerTest() {
            super("Test");
            setBounds(30,30,500,500);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setLayout(null);
    
            area = new JTextArea();
            area.setBounds(0, 0, 500, 400);
            add(area);
    
            JButton button = new JButton("Click Me!");
            button.addActionListener(this);
            button.setBounds(200, 400, 100, 40);
            add(button);
    
            // Initialization of the timer. 1 second delay and this class as ActionListener
            timer = new Timer(1000, this);
            timer.setRepeats(true); // Send events until someone stops it
            count = 0; // in the beginning, 0 events sended by timer
            running = false;
            System.out.println(timer.isRepeats());
            setVisible(true); // Shows the frame
        }
    
        public void actionPerformed(ActionEvent e) {
            if (! running) {
                timer.start();
                running = true;
            }
            // Writing the current time and increasing the cont times
            area.append(Calendar.getInstance().getTime().toString()+"\n");
            count++;
            if (count == 10) {
                timer.stop();
                count = 0;
                running = false;
            }
        }
    
        public static void main(String[] args) {
            // Executing the frame with its Timer
            new TimerTest();
        }
    }
    

    Well, this code is a sample of how to use javax.swig.Timer objects. In relation with the particular case of the question. The if statement to stop the timer must change, and, obviously, the actions of the actionPerformed. The following fragment is a skeleton of the solution actionPerformed:

    public void actionPerformed(ActionEvent e) {
        if (e.getComponent() == myDealerComponent()) {
        // I do this if statement because the actionPerformed can treat more components
            if (! running) {
                timer.start();
                runnig = true;
            }
            // Hit a card if it must be hitted
            switch (getJBTable(JB.total, JB.aces > 0)) {
              case 0:
                  JB.hit();
                  break;
              case 1:
                  break done;
              case 2:
                  JB.hit();
                  JB.bet *= 2;
                  break done;
            }
            if (JB.total >= 21) { // In this case we don't need count the number of times, only check the JB.total 21 reached
                timer.stop()
                running = false;
            }
    
        }
    }
    

    IMHO this resolves the problem, now @user920769 must think where put the actionListener and the starting/stopping conditions...

    @kleopatra: Thanks for show me the existence of this timer class, I don't know nothing about it and it's amazing, make possible a lot of tasked things into a swing application :)

提交回复
热议问题