If you do the sleep before the screen has a chance to refresh, that will happen. You may need to look into forcing the screen to refresh, first.
Could you tell me how to refresh it?
Unfortunately, I'm just learning Java myself, and haven't gotten to GUI stuff yet.
Shouldn't it will automatically refresh after the changing of the button text ?
Can we just trace the time of refreshing the GUI?
The answer to this is to simply not pause execution at all. Remember that Swing is not thread safe, so making changes to it directly from a thread can be dangerous. Anyway, to me it looks like a java.util.Timer is just fine for this task. I tried out writing in a Timer that runs a subclass of TimerTask, and it looks like it works (though it's still buggy...). These are the modifications I made:
Remember to use javax.swing.SwingUtilities.invokeLater(new Runnable()) every time you modify the GUI from outside the Event Dispatch Thread!Code:import java.util.Timer; //... if(toggleButton == 2) { Object source = event.getSource(); for(int i = 0 ; i < buttons.length ; i++) { if(source == buttons[i]) { secondEvent = i; } } buttons[secondEvent].setText(labels[secondEvent]); buttons[secondEvent].setEnabled(false); timer = new Timer(); timer.schedule(new java.util.TimerTask() { public void run() { javax.swing.SwingUtilities.invokeLater( new ButtonShower(buttons, firstEvent, secondEvent) ); } }, 1000); toggleButton = 0; if(pairMatched == 8 ) { JOptionPane.showMessageDialog(null , "Congratulation !\nYou have won!" , "Message" , JOptionPane.PLAIN_MESSAGE); } //... // Within the ActionEventHandler class: private class ButtonShower implements Runnable { public ButtonShower(JButton[] buts, int but1, int but2) { this.buts = new JButton[] { buts[but1], buts[but2] }; } public void run() { if (buts[0].getText().equals(buts[1].getText())) { pairMatched++; } else { for (JButton but : buts) { but.setText("*"); but.setEnabled(true); } } } private JButton[] buts; }
Wow I changed my sig!
ok ... i will try this out .. thanks for the help ...![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks