Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从已映射的arraylist随机获取音频文件_Java_Arrays_String_Dictionary_Jbutton - Fatal编程技术网

Java 从已映射的arraylist随机获取音频文件

Java 从已映射的arraylist随机获取音频文件,java,arrays,string,dictionary,jbutton,Java,Arrays,String,Dictionary,Jbutton,抱歉,我有一个问题需要解决,我能够用字符串数组映射文件数组,现在,我想从映射的数组列表中获取一个随机文件,以便在单击按钮时播放,但我没有得到它。。。。它不起作用 这是我的代码: private static File[] word = //Here, I announce the contents of the File array named word, about 50 Files private String []answer = //String array to be mappe

抱歉,我有一个问题需要解决,我能够用字符串数组映射文件数组,现在,我想从映射的数组列表中获取一个随机文件,以便在单击按钮时播放,但我没有得到它。。。。它不起作用

这是我的代码:

private static File[] word =
//Here, I announce the contents of the File array named word, about 50 Files

private String []answer =  
//String array to be mapped with File array         
//Other swing parameters to be used

private static JLabel sound;
private static JButton check;
private static JTextField spell;
private JButton click;

//Font to be used for all text typed in JTextField
private Font fonty;
//Icon to be displayed when sound is played
JLabel icon = new JLabel (new ImageIcon("C:\\Users\\HP\\Pictures\\Photos\\#010.jpg"));

public Quest1() {
    // Mapping of String array to File array
    Map<String, File> mapping = new HashMap();
    Map<String, File>mpl = new HashMap();

    mpl.putAll(mapping);
    //Layout for the JFrame set (the class extends JFrame)

    setLayout (new BorderLayout());
    setContentPane(icon);

    fonty = new Font("Script MT Bold", Font.PLAIN, 15);

    //JPanel to hold swing components
    JPanel hold = new JPanel ();
    hold.setLayout(new GridBagLayout());

    GridBagConstraints g = new GridBagConstraints();

    g.anchor = GridBagConstraints.WEST;
    g.gridx = 2;
    g.gridy = 1;
    g.gridwidth = 2;
    g.insets = new Insets(2, 20, 2,2);

    sound = new JLabel (new ImageIcon("C:\\Users\\HP\\Downloads\\JLabelSoundImage.jpg"));
    sound.setPreferredSize(new Dimension(70,70));
    hold.add(sound, g);

    click = new JButton ("Play");
    click.setFont(fonty);
    g.anchor = GridBagConstraints.WEST;
    g.gridx = 2;
    g.gridy = 5;
    g.gridwidth = 2;
    g.insets = new Insets(2, 20, 8, 2);
    hold.add(click, g);

    click.addActionListener (new ActionListener (){
        public void actionPerformed (ActionEvent t) {
            //Where my Problem lies.... getting a random File from the mapped array list to play when the button is clicked.
            List key = new ArrayList (mapping.keySet());
            Collections.shuffle(key);
            for (Object o: key){
                mapping.get(o);

                InputStream in = new InputStream(mapping.get(o),(word));
            }   
        }
    });

    spell = new JTextField (10);
    spell.setFont(fonty);
    g.anchor = GridBagConstraints.EAST;
    g.gridx = 5;
    g.gridy = 2;
    g.gridwidth = 3;
    g.insets = new Insets (2, 2, 2, 20);
    hold.add (spell, g);

    check = new JButton ("Check my answer");
    check.setFont(fonty);
    g.anchor = GridBagConstraints.SOUTH;
    g.gridx = 8;
    g.gridy = 8;
    g.gridwidth = 3;
    g.insets = new Insets (2, 2, 20, 2);
    hold.add (check, g);

    check.addActionListener (new ActionListener () {
        public void actionPerformed (ActionEvent e) {
            if (spell.getText().equals(mapping.get(key))) {
                String c = "Correct!";

                JOptionPane.showMessageDialog(null, c);
            }
            else {
                String d = ("Wrong! the Answer is ") + (mapping.get(key)) ;
                JOptionPane.showMessageDialog(null, d);
            }
        }
    });

    hold.setVisible(true);
    hold.setSize(400,400);
    hold.setLocation(50, 50);
    hold.setOpaque(false);

    add(hold, BorderLayout.CENTER);
}
私有静态文件[]word=
//在这里,我宣布名为word的文件数组的内容,大约50个文件
私有字符串[]答案=
//要与文件数组映射的字符串数组
//使用的其他回转参数
私人静态JLabel声音;
私有静态按钮检查;
私有静态JTextField拼写;
私人按钮点击;
//用于在JTextField中键入的所有文本的字体
私人字体字体;
//播放声音时显示的图标
JLabel图标=新的JLabel(新的图像图标(“C:\\Users\\HP\\Pictures\\Photos\\\\\ 010.jpg”);
公开问题1(){
//字符串数组到文件数组的映射
Map mapping=newhashmap();
Mapmpl=newhashmap();
mpl.putAll(映射);
//JFrame集合的布局(类扩展了JFrame)
setLayout(新的BorderLayout());
设置内容窗格(图标);
fonty=新字体(“脚本MT粗体”,Font.PLAIN,15);
//JPanel用于固定回转组件
JPanel hold=newjpanel();
hold.setLayout(新的GridBagLayout());
GridBagConstraints g=新的GridBagConstraints();
g、 锚点=GridBagConstraints.WEST;
g、 gridx=2;
g、 gridy=1;
g、 网格宽度=2;
g、 插图=新插图(2,20,2,2);
声音=新的JLabel(新的图像图标(“C:\\Users\\HP\\Downloads\\JLabelSoundImage.jpg”);
声音。设置首选尺寸(新尺寸(70,70));
按住。添加(声音,g);
单击=新建按钮(“播放”);
单击.setFont(fonty);
g、 锚点=GridBagConstraints.WEST;
g、 gridx=2;
g、 gridy=5;
g、 网格宽度=2;
g、 插图=新插图(2,20,8,2);
按住。添加(单击,g);
单击.addActionListener(新建ActionListener(){
已执行的公共无效操作(操作事件t){
//我的问题在于…从映射数组列表中获取一个随机文件,以便在单击按钮时播放。
List key=newarraylist(mapping.keySet());
收藏。洗牌(关键);
用于(对象o:键){
get(o);
InputStream in=新的InputStream(mapping.get(o),(word));
}   
}
});
拼写=新的JTextField(10);
拼写.setFont(fonty);
g、 锚点=GridBagConstraints.EAST;
g、 gridx=5;
g、 gridy=2;
g、 网格宽度=3;
g、 插图=新插图(2,2,2,20);
hold.add(拼写,g);
check=新的JButton(“检查我的答案”);
check.setFont(fonty);
g、 锚点=GridBagConstraints.SOUTH;
g、 gridx=8;
g、 gridy=8;
g、 网格宽度=3;
g、 插图=新插图(2,2,20,2);
hold.add(勾选,g);
check.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
if(spell.getText().equals(mapping.get(key))){
字符串c=“正确!”;
showMessageDialog(null,c);
}
否则{
字符串d=(“错误!答案是”)+(mapping.get(key));
showMessageDialog(null,d);
}
}
});
hold.setVisible(true);
保持。设置大小(400400);
保持设定位置(50,50);
hold.set不透明(false);
添加(保持、边框布局、中心);
}

一个问题是没有将类型提供给列表。这是非常危险的,因此请定义它实际上是什么类型的列表:

List<File> ...

您可能需要在末尾的索引中添加1。我不记得它对所有索引是包含的还是独占的。

阅读如何创建一个索引,然后编辑你的问题。嘿,你能用一种语法来表达吗,我发现很难通过。。。。谢谢你的回复!
Set<File> songs = mpl.keySet();
File[] songArray = songs.toArray();
int index = (int) (Math.random() * songArray.length);
File song = songArray[index];