I am coding a game in java. I have two jtextFields named dice and dicecom. I want such that when I press a jButton first a random value between 1 to 6 will be shown on dice, then there shall be some time gap of about 2 seconds after which a random value between 1 to 6 will be shown on dicecom. I wrote this code:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
Random rand = new Random();
int a = rand.nextInt(6);
a++;
dice.setText(String.valueOf(a));
try
{
Thread.sleep(2000);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
int b = rand.nextInt(6);
b++;
dicecom.setText(String.valueOf(b));}
But what happens is that when I press the jButton there is a two second pause in the program execution, then both values are displayed together. How do I correct my code to get the desired result?