Thread.sleep() to swing Timer conversion

荒凉一梦 提交于 2019-12-24 17:20:02

问题


I am trying to implement a Thread.sleep(6000) line but it seems to freeze in the applet. When I tried to use Timers, I wasn't sure how to use because I am not very good with event listeners. I am basically trying to call a method fetchUrl() every 6 seconds, after the user clicks the enter button. How can I implement this?

public void init() {

    c = getContentPane();
    c.setLayout(flow);
    c.setBackground(forum); 

    question.setForeground(Color.white);
    question.setFont(tnr);  
    question2.setForeground(Color.white);
    question2.setFont(tnr);
    result.setForeground(Color.white);
    result.setFont(tnr);    
    resp.setBorder(BorderFactory.createBevelBorder(0));
    timeLength.setBorder(BorderFactory.createBevelBorder(0));
    c.add(question);    
    c.add(resp);
    c.add(question2);
    c.add(timeLength);
    c.add(enter);
    c.add(result);
    resp.requestFocus();
    enter.addActionListener(this);
    t = new Timer(DELAY, this);
    t.setInitialDelay(DELAY);

}

public void actionPerformed(ActionEvent e) {
    final String n1;
    int timeMin, timeSec, count = 0, maxCount;
    timeMin = Integer.parseInt(timeLength.getText());
    timeSec = timeMin * 60;
    maxCount = (int)(timeSec/6);
    if (e.getSource() == enter) {         //user clicks enter
        n1 = resp.getText();
        while (count < maxCount) {
            fetchUrl(n1);                 //this method called every 6 seconds
            t.start();
            count++;
        }

    }
}

回答1:


First I would start by separating the ActionListener for the Timer and for the JButton.

Second nothing is happening logically with the Timer because you're swallowing it with the button source check.

Third you should understand how the timer works. Basically for every "tick" (in your case six seconds) the actionPerformed of the timer ActionListener is called. So if you want the fetch() method called, then that's what you should be visible/accessible to the in the Timer's actionPerformed.

The button's ActionListener should only handle the starting of the timer I believe. So just separate the listeners. Give each one an anonymous ActionListener and no need to make the class implement ActionListener.

For example

timer = new Timer(DELAY, new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        // do some stuff every six seconds
        fetchURL();
    }
});

enter = new JButton(...);
enter.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        timer.start();
    }
});

If you want some automatic stopping feature for the timer, you could do something like

timer = new Timer(DELAY, new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        if (someStoppingCondition()) {
            timer.stop();
        } else {
            // do some stuff every six seconds
            fetchURL();
        }
        // do some stuff every six second
    }
});



回答2:


You need to call a method after user clicks on button every 6 seconds, but you have not said how many times you want to call it.

For infinite number of times, try something like the following,

while(true){

     new Thread(){
          @Override
          public void run(){
                try{
                       Thread.sleep(6000);
                       fetchUrl(n1);
                }catch(InterruptedException e){}
          }

     }.start();

}

If you will use Thread.sleep() in your applet, then your applet will be hanged for 6 seconds and so create a new thread for it.



来源:https://stackoverflow.com/questions/24659229/thread-sleep-to-swing-timer-conversion

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