【问题标题】:Thread.sleep has the same time? [closed]Thread.sleep 有相同的时间吗? [关闭]
【发布时间】:2013-06-04 13:04:11
【问题描述】:

我正在为我的测试做一些工作,我觉得睡眠不起作用,你必须从组合框 100 毫秒 300 毫秒 500 毫秒 1000 毫秒中进行选择。但是当我启动程序并且他们这样做时,它看起来一样,也许我犯了一些错误。 提前致谢。 这是我的代码:

package vjezbanje;

import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Move extends Thread {

    JComboBox cb = new JComboBox();
    JLabel lb = new JLabel();
    JPanel pl = new JPanel();
    ImageIcon ic;
    private String[] brzine = {"100ms","300ms","500ms","1000ms"};   

    public JComboBox getCb() {
        return cb;
    }

    public void setCb(JComboBox cb) {
        this.cb = cb;
    }

    public JLabel getLb() {
        return lb;
    }

    public void setLb(JLabel lb) {
        this.lb = lb;
    }

    public JPanel getPl() {
        return pl;
    }

    public void setPl(JPanel pl) {
        this.pl = pl;
    }

    public Move() {                     
    }

    public void run() {

        pl.add(lb);
        cb = new JComboBox(brzine);

        for(int t=0; t<10; t++) {
        if(cb.getSelectedItem().equals("100ms")) {
            try {

                Random r = new Random ();
                int s = r.nextInt(6)+1;
                // 0 1 2 3 4 5
                lb.setIcon(new ImageIcon("slike\\"+s+".jpg"));
                Thread.sleep(100);
            } catch (InterruptedException e) {  
                e.printStackTrace();
            }
        }                   

        if(cb.getSelectedItem().equals("300ms")) {
            try {

                Random r = new Random ();
                int s = r.nextInt(6)+1;
                // 0 1 2 3 4 5
                lb.setIcon(new ImageIcon("slike\\"+s+".jpg"));
                Thread.sleep(300);
            } catch (InterruptedException e) {  
                e.printStackTrace();
            }   
        }       

        if(cb.getSelectedItem().equals("500ms")) {
            try {

                Random r = new Random ();
                int s = r.nextInt(6)+1;
                // 0 1 2 3 4 5
                lb.setIcon(new ImageIcon("slike\\"+s+".jpg"));
                Thread.sleep(500);          
            } catch (InterruptedException e) {  
                e.printStackTrace();
            }   
        }


        if(cb.getSelectedItem().toString().equals("1000ms")) {
            try {

                Random r = new Random ();
                int s = r.nextInt(6)+1;
                // 0 1 2 3 4 5
                lb.setIcon(new ImageIcon("slike\\"+s+".jpg"));
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }   
        }               
    }               
}   

【问题讨论】:

  • 哇。如何定义一个 long 数组,这样您就不必拥有 if 块。然后代码大小除以 4。哇。
  • 您能否再编辑您的问题以提高英语水平(建议使用大写和标点符号),并解释您不理解的输出?
  • 另外你怎么知道它们是否不同?没有计时,或等待后打印
  • 您正在让一个单独的线程休眠,这是它在结束之前所做的最后一件事。您还从 Event Dispatch Thread 之外调用 Swing 方法,所以我想知道它是如何工作的。
  • tinypic.com/r/fu5hev/5 这就是我得到的。在程序中有一个组合框,我在其中定义了睡眠长度。当我启动它时,这些骰子看起来像是在转动,但无论我选择什么速度 100 300 500 1000 ms 看起来一样。我希望这个 1 更好。抱歉英语不好:)

标签: java multithreading sleep


【解决方案1】:

同意 Gray,这应该是一个带有睡眠时间变量的块。 但是,我认为您要问的是“为什么我看不到我的图像在不同时间发生变化?”。您可能看到的只是不同时间长度的“挂起”。那是因为您在 GUI 线程上进行所有更新,包括睡眠,所以在您的整个方法返回之前实际上没有任何更新。

会有多种方式在后台更新。我可能会推荐某种允许进度更新的后台工作者。使用进度更新更改图像,并使用 worker 方法休眠。

【讨论】:

  • 谢谢,我想我现在找到了出路:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-08
  • 1970-01-01
  • 2021-11-09
  • 2013-11-02
  • 1970-01-01
  • 2010-10-01
相关资源
最近更新 更多