我对这段代码有疑问:
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
关键字。问题仍然没有解决。如何解决?