Java 甲板卡的时间延迟
我正在尝试用Java创建一个纸牌游戏。我想一张接一张地展示四张卡片,然后等几秒钟再展示另外四张卡片,以此类推。我创建了下面的代码,但卡号#4根本不显示。例如,它显示3张卡,然后等待几秒钟,然后显示其他3张卡 守则第一部分:Java 甲板卡的时间延迟,java,Java,我正在尝试用Java创建一个纸牌游戏。我想一张接一张地展示四张卡片,然后等几秒钟再展示另外四张卡片,以此类推。我创建了下面的代码,但卡号#4根本不显示。例如,它显示3张卡,然后等待几秒钟,然后显示其他3张卡 守则第一部分: class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { int hideCount = 0; addF
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);
}
}
类按钮Listener实现ActionListener{
已执行的公共无效操作(操作事件e){
int hideCount=0;
addFirstCenterLabel();
addSecondCenterLabel();
addThirdCenter标签();
addForthCenterLabel();
对于(int i=0;i<32;i++){
if(按钮[i]==e.getSource()){
如果(按钮[i]==按钮[0]| |按钮[i]==按钮[1]||
按钮[i]==按钮[2]| |按钮[i]==按钮[3]||
按钮[i]==按钮[4]| |按钮[i]==按钮[20]||
按钮[i]==按钮[21]| |按钮[i]==按钮[22]){
移除(按钮[i]);
southPanelbutton.validate();
southPanelbutton.repaint();
labelThirdCenter.setIcon(显示卡[i]);
}
}
if(按钮[i]==e.getSource()){
如果(按钮[i]==按钮[5]| |按钮[i]==按钮[6]||
按钮[i]==按钮[7]| |按钮[i]==按钮[8]||
按钮[i]==按钮[9]| |按钮[i]==按钮[23]||
按钮[i]==按钮[24]| |按钮[i]==按钮[25]){
EastPanel按钮。移除(按钮[i]);
eastPanelbutton.validate();
eastPanelbutton.repaint();
setIcon(showCard[i]);
}
}
if(按钮[i]==e.getSource()){
如果(按钮[i]==按钮[10]| |按钮[i]==按钮[11]||
按钮[i]==按钮[12]| |按钮[i]==按钮[13]||
按钮[i]==按钮[14]| |按钮[i]==按钮[26]||
按钮[i]==按钮[27]| |按钮[i]==按钮[28]){
移除(按钮[i]);
northPanelbutton.validate();
northPanelbutton.repaint();
labelSecondCenter.setIcon(显示卡[i]);
}
}
if(按钮[i]==e.getSource()){
如果(按钮[i]==按钮[15]| |按钮[i]==按钮[16]||
按钮[i]==按钮[17]| |按钮[i]==按钮[18]||
按钮[i]==按钮[19]| |按钮[i]==按钮[29]||
按钮[i]==按钮[30]| |按钮[i]==按钮[31]){
WestPanel按钮。移除(按钮[i]);
westPanelbutton.validate();
westPanelbutton.repaint();
labelFirstCenter.setIcon(showCard[i]);
hideCount++;
}
}
if(按钮[i]==e.getSource()){
如果(按钮[i]==按钮[15]| |按钮[i]==按钮[16]||
按钮[i]==按钮[17]| |按钮[i]==按钮[18]||
按钮[i]==按钮[19]| |按钮[i]==按钮[29]||
按钮[i]==按钮[30]| |按钮[i]==按钮[31]){
hideCount++;
延时();
如果(hideCount==1 | | hideCount==2 | | hideCount==3||
hideCount==4 | | hideCount==5 | | hideCount==6||
hideCount==7 | | hideCount==8){
汗腺();
}
}
}
}
}
}
守则第二部分:
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);
}
}
public void addFirstCenterLabel(){
//centerPanelLabel.add(labelFirstCenter);
//centerPanel.add(centerPanelLabel);
//下桌
setLayout(新的GridBagLayout());
c、 gridx=1;
c、 gridy=3;
添加(labelThirdCenter,c);
添加(中心面板);
}
public void addSecondCenterLabel(){
//centerPanelLabel.add(labelSecondCenter);
//centerPanel.add(centerPanelLabel);
//右表
c、 gridx=2;
c、 gridy=1;
添加(labelForthCenter,c);
添加(中心面板);
}
public void addThirdCenter标签(){
//centerPanelLabel.add(labelThirdCenter);
//centerPanel.add(centerPanelLabel);
//顶级标签
c、 gridx=1;
c、 gridy=0;
添加(labelSecondCenter,c);
添加(中心面板);
}
public void addForthCenterLabel(){
//centerPanelLabel.add(labelForthCenter);
//centerPanel.add(centerPanelLabel);
//左表
c、 gridx=0;
c、 gridy=1;
添加(labelFirstCenter,c);
添加(中心面板);
//计算游戏的回合数。每四张牌计为一张。
对于(int j=0;j在UI线程上调用Thread.sleep
不是一个好主意。它将在线程睡眠时禁用所有UI更新
我没有详细检查,但卡4的绘制似乎已经安排好了,但在线程进入睡眠状态时尚未执行。您知道如何使用调试器,不是吗?