2013-03-28 187 views
0

我有點失落,我問過我的教授同樣的問題,但我並不真正理解如何實現答案。我的代碼(下面)基本上創建了7個ImageIcon,然後我實現了一個for循環,它隨機調用圖片。這工作正常,我的問題是我如何離開每個ImageIcon 250毫秒,然後讓它關閉沒有來自用戶的輸入,然後移動到下一個圖像,直到for循環完成?在for循環中自動關閉JOptionPane

package test; 
import javax.swing.*; 

public class TEST { 

    public static void main(String[] args) { 
     //create a timer 

     //try to assign variables to pictures in an array 
     ImageIcon icon = new ImageIcon("/home/james/programmingpics/A_Guitar"); 
     JOptionPane.showMessageDialog(null, "A Note", "A Note with Guitar", 
     JOptionPane.OK_OPTION, icon);   

     ImageIcon icon1 = new ImageIcon("/home/james/programmingpics/B_Guitar"); 
     JOptionPane.showMessageDialog(null, "B Note", "B Note with Guitar", 
     JOptionPane.OK_OPTION, icon1); 


     ImageIcon icon2 = new ImageIcon("/home/james/programmingpics/C_Guitar"); 
     JOptionPane.showMessageDialog(null, "C Note", "C Note with Guitar", 
     JOptionPane.OK_OPTION, icon2); 

     ImageIcon icon3 = new ImageIcon("/home/james/programmingpics/D_Guitar"); 
     JOptionPane.showMessageDialog(null, "D Note", "D Note with Guitar", 
     JOptionPane.OK_OPTION, icon3); 

     ImageIcon icon4 = new ImageIcon("/home/james/programmingpics/E_Guitar"); 
     JOptionPane.showMessageDialog(null, "E Note", "E Note with Guitar", 
     JOptionPane.OK_OPTION, icon4); 

     ImageIcon icon5 = new ImageIcon("/home/james/programmingpics/F_Guitar"); 
     JOptionPane.showMessageDialog(null, "F Note", "F Note with Guitar", 
     JOptionPane.OK_OPTION, icon5);   

     ImageIcon icon6 = new ImageIcon("/home/james/programmingpics/G_Guitar"); 
     JOptionPane.showMessageDialog(null, "F Note", "F Note with Guitar", 
     JOptionPane.OK_OPTION, icon6); 

     ImageIcon[] iconarray = {icon, icon1, icon2, icon3, icon4, icon5, icon6}; 

     for (int i = 0; i < 1000; i++) { 
      int random = 1 * (int) ((Math.random() * 100) % 7); 
      System.out.println(iconarray[random]); 
     } 
    } 
} 
+0

在這種情況下,JDialog比JOptionPane更有用。 – 2013-03-28 02:49:09

+0

我對任何能夠使這個工作起作用的東西都沒問題,我對JDialog還不熟悉。 – user2154095 2013-03-28 04:35:22

回答

0

您可以使用java.awt.Robot中的「​​機器人」級和一個計時器simule「用戶的開關輸入」,並在一段時間後關閉的JOptionPane。