java - How to sleep a button setText? -
hey guys trying make game of matching cards. if 2 cards match user gets point , cards stay visible, otherwise flip them (or settext("")) did research swing sleep wasn't sure how implement in code. have tried can't work. have code running in main.
actionlistener buttonlistener = new actionlistener() { @override public void actionperformed(actionevent e) { jbutton selectedbutton = (jbutton)e.getsource(); (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 cast 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(""); } } } } } } };
i "think" should doing more like...
timer timer = new timer(100, new actionlistener() { @override public void actionperformed(actionevent evt) { //pop stack coordinates , set them "" //settext on button clicked "" system.out.println(cardlist.getcardnode(row, col).getletter()); } }); timer.setrepeats(false); timer.start();
which will, after delay of 100 milliseconds, call actionlistener
's actionperformed
method, allowing reset state of ui...
problem inside loop , have access row , col clicked
then create actionlistener
take required information , act upon when actionperformed
method called...
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(""); } }
then use timer
...
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();
Comments
Post a Comment