JAVA添加图片本来好好的能运行,想添加个音乐,音乐没弄好,反倒背景图片也不对了,求大神看看我的程序
package贪吃蛇;importjava.awt.*;importjava.awt.image.BufferedImage;importjava.awt.image.I...
package 贪吃蛇;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;
import java.applet.*;
import javax.imageio.*;
import javax.sound.sampled.Clip;
public class Snake {
public static void main(String[] args) {
JFrame f = new JFrame("简约贪吃蛇");
JButton b1 = new JButton("开始游戏");
JButton b2 = new JButton("退出游戏");
JLabel l1 = new JLabel("简约贪吃蛇");
f.setLayout(null);
// 未进行归类的方法
l1.setFont(new Font("楷体",0,30));
ImagePanel p = new ImagePanel() ;//*******这句Eclipse显示有错*********
f.setBounds(350, 150, 600, 500);
b1.setBounds(239,250,100,32);
b2.setBounds(239,284,100,32);
p.setBounds(0,0,600,500);
l1.setBounds(210, 70, 300, 100);
f.add(b1, f);
f.add(b2, f);
f.add(l1, f);
f.getContentPane().add(p);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.setVisible(true);
}
//开始界面的背景图片
class ImagePanel extends JPanel{
public void paint(Graphics g){
String imagepath="1.jpg";
BufferedImage img = null;
try {
URL image=this.getClass().getResource(imagepath);
img = ImageIO.read(image);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g.drawImage(img, 0, 0, 600, 500, null);
}
}
}
-------------我打*的地方是Eclipse中显示有错误的地方------------- 展开
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;
import java.applet.*;
import javax.imageio.*;
import javax.sound.sampled.Clip;
public class Snake {
public static void main(String[] args) {
JFrame f = new JFrame("简约贪吃蛇");
JButton b1 = new JButton("开始游戏");
JButton b2 = new JButton("退出游戏");
JLabel l1 = new JLabel("简约贪吃蛇");
f.setLayout(null);
// 未进行归类的方法
l1.setFont(new Font("楷体",0,30));
ImagePanel p = new ImagePanel() ;//*******这句Eclipse显示有错*********
f.setBounds(350, 150, 600, 500);
b1.setBounds(239,250,100,32);
b2.setBounds(239,284,100,32);
p.setBounds(0,0,600,500);
l1.setBounds(210, 70, 300, 100);
f.add(b1, f);
f.add(b2, f);
f.add(l1, f);
f.getContentPane().add(p);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.setVisible(true);
}
//开始界面的背景图片
class ImagePanel extends JPanel{
public void paint(Graphics g){
String imagepath="1.jpg";
BufferedImage img = null;
try {
URL image=this.getClass().getResource(imagepath);
img = ImageIO.read(image);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g.drawImage(img, 0, 0, 600, 500, null);
}
}
}
-------------我打*的地方是Eclipse中显示有错误的地方------------- 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询