如何解决JLabel不会在Thread.sleep/ this.wait之前更改其icon属性
问题简介: 我正在研究与JFrame相关的Aplication。在某些时候,我必须:
-
标签的
- 更改图标
- 停止过程
- 等待
- 恢复过程
问题从一开始就出现。如果我暂停应用-JLabel图标将不会更改。 目前,我尝试了几种暂停方法,包括: Thread.sleep(), this.wait()和带有 synchronize 的变体。我还尝试将 .updateUI()放置在每个适当的位置,而不是放置位置。问题本身对我毫无意义。
这是我的主要空白代码段:
private void checkCard(java.awt.event.MouseEvent evt,int y,int x){
if(this.memory.getGrid()[y][x]>0){
clicks++;
this.specialIcon(y,x);
switch (clicks) {
case 1:
pos1[0]=y;
pos1[1]=x;
break;
case 2:
this.labelCards[pos2[0]][pos2[1]].updateUI();
this.grid.updateUI();
this.core.updateUI();
pos2[0]=y;
pos2[1]=x;
this.memory.play(pos1,pos2);
//CHECK POS1
//CHECK POS2
if(!memory.getSolved(pos2)){
generateThreadWait();
this.labelCards[pos2[0]][pos2[1]].setIcon(new ImageIcon(this.src+"unknown.png"));
this.labelCards[pos1[0]][pos1[1]].setIcon(new ImageIcon(this.src+"unknown.png"));
} else if(memory.getSolved(pos2)){
this.labelCards[pos2[0]][pos2[1]].setIcon(new javax.swing.ImageIcon(this.src+"solved.png"));
this.labelCards[pos2[0]][pos2[1]].addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
//DEBUG
//System.out.println("DISABLED");
}
});
this.labelCards[pos1[0]][pos1[1]].setIcon(new javax.swing.ImageIcon(this.src+"solved.png"));
this.labelCards[pos1[0]][pos1[1]].addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
//DEBUG
//System.out.println("DISABLED");
}
});
};
this.clicks =0;
break;
default:
break;
}
}
}
和“ generateThreadWait()” 无效:
private synchronized void generateThreadWait() {
final java.time.format.DateTimeFormatter dtf = java.time.format.DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
try {
System.out.println("Inside synchronized block entry..." + dtf.format(java.time.LocalDateTime.now()));
this.wait(2000);
System.out.println("Inside synchronized block exit..." + dtf.format(java.time.LocalDateTime.now()));
} catch (InterruptedException e) {
e.printStackTrace();
}
任何想法我都会很高兴。谢谢!!!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。