我正在尝试用Java创建一个甲板游戏。我想一个接一个地显示四张牌,然后等待几秒钟再显示另外四张牌,依此类推。我在下面创建了代码,但卡号#4根本没有显示。例如,它显示3张卡然后等待几秒钟,然后显示其他3张卡。
代码的第一部分:
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
int hideCount = 0;
addFirstCenterLabel();
addSecondCenterLabel();
addThirdCenterLabel();
addForthCenterLabel();
for (int i = 0 ; i < 32 ; i++) {
if (button[i] == e.getSource()) {
if (button[i] == button[0] || button[i] == button[1] ||
button[i] == button[2] || button[i] == button[3] ||
button[i] == button[4] || button[i] == button[20] ||
button[i] == button[21] || button[i] == button[22]) {
southPanelbutton.remove(button[i]);
southPanelbutton.validate();
southPanelbutton.repaint();
labelThirdCenter.setIcon(showCard[i]);
}
}
if (button[i] == e.getSource()) {
if (button[i] == button[5] || button[i] == button[6] ||
button[i] == button[7] || button[i] == button[8] ||
button[i] == button[9] || button[i] == button[23] ||
button[i] == button[24] || button[i] == button[25]) {
eastPanelbutton.remove(button[i]);
eastPanelbutton.validate();
eastPanelbutton.repaint();
labelForthCenter.setIcon(showCard[i]);
}
}
if (button[i] == e.getSource()) {
if (button[i] == button[10] || button[i] == button[11] ||
button[i] == button[12] || button[i] == button[13] ||
button[i] == button[14] || button[i] == button[26] ||
button[i] == button[27] || button[i] == button[28]) {
northPanelbutton.remove(button[i]);
northPanelbutton.validate();
northPanelbutton.repaint();
labelSecondCenter.setIcon(showCard[i]);
}
}
if (button[i] == e.getSource()) {
if (button[i] == button[15] || button[i] == button[16] ||
button[i] == button[17] || button[i] == button[18] ||
button[i] == button[19] || button[i] == button[29] ||
button[i] == button[30] || button[i] == button[31]) {
westPanelbutton.remove(button[i]);
westPanelbutton.validate();
westPanelbutton.repaint();
labelFirstCenter.setIcon(showCard[i]);
hideCount++;
}
}
if (button[i] == e.getSource()) {
if (button[i] == button[15] || button[i] == button[16] ||
button[i] == button[17] || button[i] == button[18] ||
button[i] == button[19] || button[i] == button[29] ||
button[i] == button[30] || button[i] == button[31]) {
hideCount++;
timeDelay();
if (hideCount == 1 || hideCount == 2 || hideCount == 3 ||
hideCount == 4 || hideCount == 5 || hideCount == 6 ||
hideCount == 7 || hideCount == 8) {
hideCards();
}
}
}
}
}
}
代码的第二部分:
public void addFirstCenterLabel() {
// centerPanelLabel.add(labelFirstCenter);
// centerPanel.add(centerPanelLabel);
// down table
centerPanel.setLayout(new GridBagLayout());
c.gridx = 1;
c.gridy = 3;
centerPanel.add(labelThirdCenter, c);
add(centerPanel);
}
public void addSecondCenterLabel() {
// centerPanelLabel.add(labelSecondCenter);
// centerPanel.add(centerPanelLabel);
// right table
c.gridx = 2;
c.gridy = 1;
centerPanel.add(labelForthCenter, c);
add(centerPanel);
}
public void addThirdCenterLabel() {
// centerPanelLabel.add(labelThirdCenter);
// centerPanel.add(centerPanelLabel);
// top label
c.gridx = 1;
c.gridy = 0;
centerPanel.add(labelSecondCenter, c);
add(centerPanel);
}
public void addForthCenterLabel() {
// centerPanelLabel.add(labelForthCenter);
// centerPanel.add(centerPanelLabel);
// left table
c.gridx = 0;
c.gridy = 1;
centerPanel.add(labelFirstCenter, c);
add(centerPanel);
// to count the round of the game. each four cards count as 1.
for (int j = 0 ; j <= 7 ; j++) {
if (rounds[j] == 0) {
rounds[j] = 1;
break;
}
}
}
代码的第三部分:
public void hideCards() {
for (int j = 0 ; j <= 7 ; j++) {
// System.out.println(rounds[j]);
// if the round ==1, make the four cards unvisible
if (rounds[j] == 1) {
labelThirdCenter.setIcon(emptyCard[0]);
labelForthCenter.setIcon(emptyCard[0]);
labelSecondCenter.setIcon(emptyCard[0]);
labelFirstCenter.setIcon(emptyCard[0]);
}
}
}
public void timeDelay() {
try {
Thread.sleep(2500); // one second
}
catch (InterruptedException e) {
throw new RuntimeException("Don't know how to handle this", e);
}
}
答案 0 :(得分:0)
在UI线程上调用Thread.sleep
不是一个好主意。它将在线程休眠时禁用所有UI更新。
我没有详细检查,但似乎卡4的图纸是预定的,但是当线程进入睡眠状态时尚未执行。