package shooterGuy;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Display extends JPanel{
int width = 800;
int height = 800;
JFrame j;
public void run(){
j = new JFrame("Shooter Guy");
j.setVisible(true);
j.setSize(width, height);
j.setResizable(true);
j.setLocationRelativeTo(null);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loadPics();
}
public void paintComponent(Graphics g){
g.drawImage(bg, 0, 0, null);
}
Image bg;
public void loadPics(){
bg = new ImageIcon("K:\files\jamiesstuff\java\Java Pictures\Back.png").getImage();
}
}
这是我的显示类,它强调“K:\ files \ jamiesstuff \ java \ Java Pictures \ Back.png”,并说错误是“无效的转义序列(有效的是\ b \ t \ n \ f \ r \“\'\)”我不知道这意味着什么或如何解决它。我在Mac上做了同样的事情并且工作正常。请告诉我如何解决这个错误。
答案 0 :(得分:2)
您必须转义反斜杠或使用斜杠作为路径分隔符:
bg = new ImageIcon("K:\\files\\jamiesstuff\\Java\\Java Pictures\\Back.png").getImage();
或
bg = new ImageIcon("K:/files/jamiesstuff/java/Java Pictures/Back.png").getImage();