我对此代码有疑问:
package javaapplication16;
import java.io.InputStream;
import javax.swing.JOptionPane;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class JavaApplication16 {
public static void main(String[] args) {
NewJFrame n = new NewJFrame();
n.setVisible(true);
InputStream is;
is = this.getClass().getClassLoader().getResourceAsStream("samp.wav");
try {
AudioStream audioStream;
audioStream = new AudioStream(is);
AudioPlayer.player.start(audioStream);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
}
就是说
error: non-static variable this cannot be referenced from a static context
is = this.getClass().getClassLoader().getResourceAsStream("samp.wav");
如果我将InputStream变量设为静态,那么它会告诉我表达式的非法开头。我还删除了
this
关键字。问题仍然没有解决。如何解决? 最佳答案
只是避免出现问题,就像这样:
JavaApplication16.class.getClassLoader().getResourceAsStream("samp.wav");