0

我有点迷茫,我问过我的教授同样的问题,我真的不明白如何实施答案。我的代码(如下)基本上创建了 7 个 ImageIcon,然后我实现了一个 for 循环,它随机调用图片。这很好用,我的问题是如何让每个 ImageIcon 保持 250 毫秒,然后在没有用户输入的情况下关闭它,然后转到下一个图像直到 for 循环完成?

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]);
        }
    }
}
4

1 回答 1

0

您可以使用 Java.awt.robot 中的“机器人”类和计时器来模拟“用户输入”并在一段时间后关闭 JOptionPane。

于 2015-06-21T22:39:46.930 回答