gpt4 book ai didi

java - 错误 : non-static variable this cannot be referenced from a static context

转载 作者:行者123 更新时间:2023-11-29 07:11:22 25 4
gpt4 key购买 nike

这段代码有问题:

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");

关于java - 错误 : non-static variable this cannot be referenced from a static context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13593548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com