簡體   English   中英

在for循環中自動關閉JOptionPane

[英]Auto-close a JOptionPane in a for loop

我有點迷茫,我問過我的教授同樣的問題,但我不太了解如何實施答案。 我的代碼(如下)基本上創建了7個ImageIcon,然后實現了一個for循環,在其中它隨機調用圖片。 效果很好,我的問題是如何讓每個ImageIcon保持250ms,然后在沒有用戶輸入的情況下將其關閉,然后移至下一個圖像,直到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]);
        }
    }
}

您可以使用Java.awt.robot中的“​​機器人”類和計時器來模擬“用戶的輸入”並在一段時間后關閉JOptionPane。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM