java - How to set a timer to an ActionListener -
i have created slot machine game. in slot machine game have button generate 3 random images when pressed. want button generate 3 random images 5 seconds after pressed. here code button , action listener.
b1.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { randompicturegenerator(evt); repaint(); } }); private void randompicturegenerator(java.awt.event.actionevent evt) { this.run(); } public void run() { pictures = new arraylist<>(); pictures.add(new file("question.png")); pictures.add(new file("banana.png")); pictures.add(new file("chocolate.png")); pictures.add(new file("icecream.png")); pictures.add(new file("bell.png")); pictures.add(new file("apple.png")); pictures.add(new file("money.png")); pictures.add(new file("number-7.png")); pictures.add(new file("necklace.png")); pictures.add(new file("gloves.png")); int number = rand.nextint((pictures.size() -1/*max*/ - 0/*min*/) + 1) + 0/*min*/; this.label.seticon(new imageicon(pictures.get(number).getabsolutepath())); int number2 = rand2.nextint((pictures.size() -1/*max*/ - 0/*min*/) + 1) + 0/*min*/; this.label2.seticon(new imageicon(pictures.get(number2).getabsolutepath())); int number3 = rand3.nextint((pictures.size() -1/*max*/ - 0/*min*/) + 1) + 0/*min*/; this.label3.seticon(new imageicon(pictures.get(number3).getabsolutepath())); }
this threads come play! have lot of reading compiler errors through exception handling, threading , thread interruptions. can want putting code in run() method.
try { thread.sleep(5000); //5000 milliseconds 5 seconds. } catch(interruptedexception ex) { thread.currentthread().interrupt(); }
Comments
Post a Comment