Java程序说声音文件不存在

Java程序说声音文件不存在,java,swing,audio,Java,Swing,Audio,我目前正在用Java编写一个钢琴的代码,我曾经有一个完整的类,当按下一个音符时,该类中的一个特定方法被称为该特定音符。为了缩短我的代码(仍在处理该部分),我使用了我制作的boolean数组的整数来决定注释。例如,如果boolean[1]设置为true,我将使用整数调用该方法,并基于该整数,将字符串设置为声音文件的名称。我遇到的问题是,它不断地说文件不在那里,我知道事实上它在那里。有人能帮我吗 import java.awt.event.*; //imports import java.awt.C

我目前正在用Java编写一个钢琴的代码,我曾经有一个完整的类,当按下一个音符时,该类中的一个特定方法被称为该特定音符。为了缩短我的代码(仍在处理该部分),我使用了我制作的
boolean
数组的整数来决定注释。例如,如果
boolean[1]
设置为
true
,我将使用整数调用该方法,并基于该整数,将字符串设置为声音文件的名称。我遇到的问题是,它不断地说文件不在那里,我知道事实上它在那里。有人能帮我吗

import java.awt.event.*; //imports
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Font;
import javax.swing.*;
import java.io.File;
import java.util.Arrays;
import javax.sound.sampled.*;


public class PianoGame extends JFrame{ //JFrame class
    public PianoGame(){
        super("PianoGame");
        setSize(1000,600);
        setLocation(400,50);
        setResizable(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        PianoGameContent pgb = new PianoGameContent();
        setContentPane(pgb);
        setVisible(true);
    }
    public static void main(String[]args){
        PianoGame pg  = new PianoGame();
    }
}
class PianoGameContent extends JPanel implements KeyListener { //JPanel Class
    boolean[] notes = new boolean [30];
    File [] snotes = new File [30];

    public PianoGameContent() {
        boolean[] notes = new boolean [30];
        Arrays.fill(notes, Boolean.FALSE);
        addKeyListener(this);
        setFocusable(true);
        requestFocusInWindow();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Font f = new Font("Serif", Font.BOLD, 50); //draw Piano
        g.setFont(f);
        for (int i = 1; i < 18; i++){
            if(notes[i] == true){
                g.setColor(Color.RED);
                g.fillRect(50*i, 200, 50, 300);
                g.setColor(Color.BLACK);
                play(i);
            }
            else{
                g.setColor(Color.WHITE);
                g.fillRect(50*i, 200, 50, 300);
                g.setColor(Color.BLACK);
            }
        }
        g.setColor(Color.BLACK);
        for (int i = 1; i<18; i++)
        g.drawRect(50*i, 200, 50, 300);

        if (notes[18] == true) {
            g.setColor(Color.RED);
            g.fillRect(85, 200, 30, 175);
            play(18);
            g.setColor(Color.BLACK);

        } else {
            g.setColor(Color.BLACK);
            g.fillRect(85, 200, 30, 175);
        }

        if (notes[19] == true) {
            g.setColor(Color.RED);
            g.fillRect(135, 200, 30, 175);
            play(19);
            g.setColor(Color.BLACK);

        } else {
            g.setColor(Color.BLACK);
            g.fillRect(135, 200, 30, 175);
        }
        if (notes[20] == true) {
            g.setColor(Color.RED);
            g.fillRect(235, 200, 30, 175);
            g.setColor(Color.BLACK);

        } else {
            g.setColor(Color.BLACK);
            g.fillRect(235, 200, 30, 175);
        }
        if (notes[21] == true) {
            g.setColor(Color.RED);
            g.fillRect(285, 200, 30, 175);
            g.setColor(Color.BLACK);

        } else {
            g.setColor(Color.BLACK);
            g.fillRect(285, 200, 30, 175);
        }
        if (notes[22] == true) {
            g.setColor(Color.RED);
            g.fillRect(335, 200, 30, 175);
            g.setColor(Color.BLACK);

        } else {
            g.setColor(Color.BLACK);
            g.fillRect(335, 200, 30, 175);
        }

        if (notes[23] == true) {
            g.setColor(Color.RED);
            g.fillRect(435, 200, 30, 175);
            g.setColor(Color.BLACK);

        } else {
            g.setColor(Color.BLACK);
            g.fillRect(435, 200, 30, 175);
        }

        if (notes[24] == true) {
            g.setColor(Color.RED);
            g.fillRect(485, 200, 30, 175);
            g.setColor(Color.BLACK);

        } else {
            g.setColor(Color.BLACK);
            g.fillRect(485, 200, 30, 175);
        }
        if (notes[25] == true) {
            g.setColor(Color.RED);
            g.fillRect(585, 200, 30, 175);
            g.setColor(Color.BLACK);

        } else {
            g.setColor(Color.BLACK);
            g.fillRect(585, 200, 30, 175);
        }
        if (notes[26] == true) {
            g.setColor(Color.RED);
            g.fillRect(635, 200, 30, 175);
            g.setColor(Color.BLACK);

        } else {
            g.setColor(Color.BLACK);
            g.fillRect(635, 200, 30, 175);
        }
        if (notes[27] == true) {
            g.setColor(Color.RED);
            g.fillRect(685, 200, 30, 175);
            g.setColor(Color.BLACK);

        } else {
            g.setColor(Color.BLACK);
            g.fillRect(685, 200, 30, 175);
        }
        if (notes[28] == true) {
            g.setColor(Color.RED);
            g.fillRect(785, 200, 30, 175);
            g.setColor(Color.BLACK);

        } else {
            g.setColor(Color.BLACK);
            g.fillRect(785, 200, 30, 175);
        }
        if (notes[29] == true) {
            g.setColor(Color.RED);
            g.fillRect(835, 200, 30, 175);
            g.setColor(Color.BLACK);

        } else {
            g.setColor(Color.BLACK);
            g.fillRect(835, 200, 30, 175);
        }
        g.drawRect(85, 200, 30, 175);
        g.drawRect(135, 200, 30, 175);
        g.drawRect(235, 200, 30, 175);
        g.drawRect(285, 200, 30, 175);
        g.drawRect(335, 200, 30, 175);
        g.drawRect(435, 200, 30, 175);
        g.drawRect(485, 200, 30, 175);
        g.drawRect(585, 200, 30, 175);
        g.drawRect(635, 200, 30, 175);
        g.drawRect(685, 200, 30, 175);
        g.drawRect(785, 200, 30, 175);
        g.drawRect(835, 200, 30, 175);

    }

    public void keyPressed(KeyEvent e) { //keypress sets assigned variable to true
        char x;
        x = e.getKeyChar();
        if (e.getKeyCode() == KeyEvent.VK_1)
            notes [18] = true;
        if (e.getKeyCode() == KeyEvent.VK_2)
            notes [19] = true;
        if (e.getKeyCode() == KeyEvent.VK_3)
            notes [20] = true;
        if (e.getKeyCode() == KeyEvent.VK_4)
            notes [21] = true;
        if (e.getKeyCode() == KeyEvent.VK_5)
            notes [22] = true;
        if (e.getKeyCode() == KeyEvent.VK_6)
            notes [23] = true;
        if (e.getKeyCode() == KeyEvent.VK_7)
            notes [24] = true;
        if (e.getKeyCode() == KeyEvent.VK_8)
            notes [25] = true;
        if (e.getKeyCode() == KeyEvent.VK_9)
            notes [26] = true;
        if (e.getKeyCode() == KeyEvent.VK_0)
            notes [27] = true;
        if (x == 'q')
            notes [1] = true;
        if (x == 'w')
            notes [2]= true;
        if (x == 'e')
            notes [3] = true;
        if (x == 'r')
            notes [4] = true;
        if (x == 't')
            notes [5] = true;
        if (x == 'y')
            notes [6] = true;
        if (x == 'u')
            notes [7] = true;
        if (x == 'i')
            notes[8] = true;
        if (x == 'o')
            notes[9] = true;
        if (x == 'p')
            notes[10] = true;
        if (x == 'a')
            notes [11] = true;
        if (x == 's')
            notes [12] = true;
        if (x == 'd')
            notes [13] = true;
        if (x == 'f')
            notes [14] = true;
        if (x == 'g')
            notes [15] = true;
        if (x == 'h')
            notes [16] = true;
        if (x == 'j')
            notes [17] = true;
        if (x == 'k')
            notes [28]= true;
        if (x == 'l')
            notes [29] = true;
        repaint();

    }
    public void keyReleased(KeyEvent e) { //when released everything gets reset to false
        Arrays.fill(notes, Boolean.FALSE);
        repaint();
    }
    public void keyTyped(KeyEvent e) {
    }

    public void play(int decide){
        String x = "c4.wav";
        if (decide == 1)
            x = "c4.wav";
        if (decide == 2)
            x = "d4.wav";
        if (decide == 3)
            x = "e4.wav";
        if (decide == 4)
            x = "f4.wav";
        if (decide == 5)
            x = "g4.wav";
        if (decide == 6)
            x = "a4.wav";
        if (decide == 7)
            x = "b4.wav";
        if (decide == 8)
            x = "c5.wav";
        if (decide == 9)
            x = "d5.wav";
        if (decide == 10)
            x = "e5.wav";
        if (decide == 11)
            x = "f5.wav";
        if (decide == 12)
            x = "g5.wav";
        if (decide == 13)
            x = "a5.wav";
        if (decide == 14)
            x = "b5.wav";
        if (decide == 15)
            x = "c6.wav";
        if (decide == 16)
            x = "d6.wav";
        if (decide == 17)
            x = "e6.wav";
        if (decide == 18)
            x = "db4.wav";
        if (decide == 19)
            x = "eb4.wav";

        try {
            File csound = new File(x);
            Clip clip = AudioSystem.getClip();
            clip.open(AudioSystem.getAudioInputStream(csound));
            clip.start();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
}
导入java.awt.event.*//进口
导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.Font;
导入javax.swing.*;
导入java.io.File;
导入java.util.array;
导入javax.sound.sampled.*;
公共类PianoGame扩展了JFrame{//JFrame类
公共钢琴{
超级(“钢琴”);
设置大小(1000600);
设定位置(400,50);
可设置大小(真);
setDefaultCloseOperation(在关闭时处理);
PianoGameContent pgb=新PianoGameContent();
setContentPane(pgb);
setVisible(真);
}
公共静态void main(字符串[]args){
PianoGame pg=新PianoGame();
}
}
类PianoGameContent扩展了JPanel实现了KeyListener{//JPanel类
布尔值[]注释=新布尔值[30];
文件[]snotes=新文件[30];
公共钢琴内容(){
布尔值[]注释=新布尔值[30];
数组.fill(注释,布尔值.FALSE);
addKeyListener(此);
设置聚焦(真);
requestFocusInWindow();
}
公共组件(图形g){
超级组件(g);
Font f=新字体(“衬线”,Font.BOLD,50);//绘制钢琴
g、 setFont(f);
对于(int i=1;i<18;i++){
如果(注释[i]==真){
g、 setColor(Color.RED);
g、 fillRect(50*i,200,50,300);
g、 设置颜色(颜色为黑色);
游戏(一);
}
否则{
g、 setColor(Color.WHITE);
g、 fillRect(50*i,200,50,300);
g、 设置颜色(颜色为黑色);
}
}
g、 设置颜色(颜色为黑色);

对于(int i=1;i更改
System.err.println(e.getMessage());
e.printStackTrace();
并复制/粘贴(而不是改写)输出。作为旁白:1)如果使用示例进行注释,则在启动时而不是需要时加载所有示例。2)但这似乎是MIDI的工作。可能需要使用Java MIDI系统中已有的乐器库。您不必为问题提供全部代码,只需将出现错误的部分放入即可。无需设计部分。了解与项目相关的文件存储位置也会有所帮助,目前,您的代码希望它们位于代码执行所在的同一目录中,这与类文件的位置不同,因此请注意,我发现这就像@MadProgrammer所说的。我一开始忘了/src谢谢!我还有一个问题,如果你按下一个键,它会继续播放。有没有办法让你按下一个键时,它不会一直像按键一样,并让它等待键真正复位?