我有一个播放音频的程序,同时使用JFrame请求用户输入。如果用户输入正确的响应(31)3次,那么程序将继续代码的其他方面(我没有包括它,因为它不相关)。但是,如果用户未能在30秒内提供3次正确的响应,则该对话框应该关闭,然后继续另一个方面。在以下程序中,如果用户未能输入正确的响应,则JFrame对话框将继续显示。如何在30秒内关闭JFrame。如果我可以在30秒后关闭JFrame,我计划在关闭后将countconsolablecry1硬编码为3,以便它可以通过while循环并继续执行程序。请帮我关闭对话框。
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();}}}