我正在为我的测试做一些工作,我觉得睡眠不起作用,你必须从组合框 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();
}
}
}
}