I have a program that plays an audio and at the same time asks for user input using JFrame. If the user inputs a correct response (31) 3 times, then the program will continue with other aspects of the code (I haven't included this as it is not relevant). However, if the user fails to provide the correct response 3 times within 30 seconds, the dialog is supposed to close and then continue with the other aspect. In the following program, if the user fails to enter correct response, the JFrame dialog continues to show up. How do I make the JFrame close in 30 second. If I can make the JFrame close after 30 seconds, I plan on hardcoding the countconsolablecry1 to 3 after the closing so that it can pass the while loop and continue with the program. Please help me close the dialog.
public class crytask {
public static void main(String args[]) throws InterruptedException, IOException {
Runnable consolablecry1 = new consolablecry();
final Thread consolablethread1 = new Thread(consolablecry1);
consolablethread1.start();
final long timestartconsolablecry1 = System.nanoTime();
int countconsolablecry1 = 0;
TimeUnit.SECONDS.sleep(5);
while(countconsolablecry1!=3){
JFrame frameconsolablecry1 = new JFrame();
Object resultconsolablecry1 = JOptionPane.showInputDialog(frameconsolablecry1, "Enter sequence:");
String inputconsolablecry1 = resultconsolablecry1.toString();
if (inputconsolablecry1.equalsIgnoreCase("31")) {
countconsolablecry1 = countconsolablecry1 +1;}}
consolablethread1.interrupt();
final long timeendconsolablecry1 = System.nanoTime();
System.out.println("this thread is cancelled");
TimeUnit.SECONDS.sleep(45-(timeendconsolablecry1-timestartconsolablecry1)/1000000000);
}}
class consolablecry implements Runnable {
@Override
public void run() {
Clip clip = null;
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("/Users/babe/Desktop/Con1.wav").getAbsoluteFile());
clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
Thread.sleep(clip.getMicrosecondLength() / 1000);
TimeUnit.SECONDS.wait(5);
} catch(InterruptedException ex) {
clip.stop();
System.out.println("Cancelled 1!");
} catch (Exception ex) {
System.out.println("Error with playing sound.");
ex.printStackTrace();}}}