Java 如何睡眠一个按钮?
嘿,伙计们,我正试着做一个配对卡片的游戏。 若两张卡片匹配,用户得到一个点,卡片保持可见,否则翻转它们(或setText(“”),我做了关于swing sleep的研究,但我不确定如何在代码中实现它。我什么都试过了,但都没能成功。我在main中运行了这段代码Java 如何睡眠一个按钮?,java,swing,actionlistener,Java,Swing,Actionlistener,嘿,伙计们,我正试着做一个配对卡片的游戏。 若两张卡片匹配,用户得到一个点,卡片保持可见,否则翻转它们(或setText(“”),我做了关于swing sleep的研究,但我不确定如何在代码中实现它。我什么都试过了,但都没能成功。我在main中运行了这段代码 ActionListener buttonListener = new ActionListener() { @Override public void actionPerformed(ActionEven
ActionListener buttonListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
JButton selectedButton = (JButton)e.getSource();
for (int row = 0; row < 6;row++){
for(int col = 0; col < 6; col++){
if (buttons[row][col] == selectedButton){
flipCard(row, col);
if(stack.empty()){
stack.push(row+","+col);
}else{
String word = (String)stack.pop();
String[] ar = word.split(",");
System.out.println(ar[0] + " " + ar[1]);
if (cardList.getCardNode(row, col).getLetter() ==
cardList.getCardNode(Integer.parseInt(ar[0]),
Integer.parseInt(ar[1])).getLetter()){
System.out.println("equal");
}else{
System.out.println("not equal");
//Compiler complains
//Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.Timer cannot be cast to javax.swing.JButton
Timer timer = new Timer(100 ,this);
timer.setRepeats(false);
timer.start();
buttons[row][col].setText("");
buttons[Integer.parseInt(ar[0])]
[Integer.parseInt(ar[1])].setText("");
}
}
}
}
}
}
};
ActionListener按钮Listener=new ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e)
{
JButton selectedButton=(JButton)e.getSource();
用于(int行=0;行<6;行++){
for(int col=0;col<6;col++){
如果(按钮[行][列]==选定按钮){
动画卡(行、列);
if(stack.empty()){
堆栈推送(行+“,”+col);
}否则{
字符串字=(字符串)stack.pop();
字符串[]ar=word.split(“,”);
System.out.println(ar[0]+“”+ar[1]);
if(cardList.getCardNode(行,列).getLetter()==
getCardNode(Integer.parseInt(ar[0]),
Integer.parseInt(ar[1]).getLetter(){
系统输出打印项次(“相等”);
}否则{
System.out.println(“不相等”);
//编译器抱怨
//线程“AWT-EventQueue-0”java.lang.ClassCastException中的异常:javax.swing.Timer无法转换为javax.swing.JButton
定时器=新定时器(100,本);
timer.setRepeats(假);
timer.start();
按钮[行][col].setText(“”);
按钮[Integer.parseInt(ar[0])]
[Integer.parseInt(ar[1])].setText(“”);
}
}
}
}
}
}
};
我“认为”你应该做的更像是
Timer timer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
//Pop stack coordinates and set them back to ""
//setText on button clicked to ""
System.out.println(cardList.getCardNode(row, col).getLetter());
}
});
timer.setRepeats(false);
timer.start();
它将在延迟100毫秒后调用ActionListener
的actionPerformed
方法,允许您重置UI的状态
问题是我在循环中,单击时只能访问行和列 然后创建一个
ActionListener
,当调用actionPerformed
方法时,它获取所需的信息并对其采取行动
public class FlipperHandler implements ActionListener {
private JButton[] buttons;
private int[] card1, card2;
public FlipperHandler(JButton[] buttons, int[] card1, int[] card2) {
this.buttons = buttons;
this.card1 = card1;
this.card2 = card2;
}
@Override
public void actionPerformed(ActionEvent evt) {
buttons[card1[0]][card1[1]].setText("");
buttons[card2[0]][card2[2]].setText("");
}
}
然后将其与计时器一起使用
Timer timer = new Timer(100, new FlipperHandler(buttons,
new int[]{row, col},
new int[]{Integer.parseInt(ar[0]), Integer.parseInt(ar[1])});
timer.setRepeats(false);
timer.start();
什么是任务执行者?NVM-为了更快地获得更好的帮助,发布一个(最小完整的可验证示例)或(简短、自包含、正确的示例)。此外,源代码中的一行空白就是所需的全部内容。
{
之后或}
之前的空行通常也是多余的。“//编译器抱怨”-它说什么?你的意思是什么?现在我想看一个能证明你的问题的例子。这不是一个代码转储,而是您正在做的一个示例,它突出了您所遇到的问题。这将减少混乱和更好的响应问题是我在循环中,单击时只能访问行和列。创建一个ActionListener
类,该类获取重置状态所需的信息谢谢先生@madProgrammer