关于java中添加图片的问题,图片显示不了
importjava.net.URL;importjavax.swing.*;importjava.awt.*;publicclassimageextendsJFrame...
import java.net.URL;
import javax.swing.*;
import java.awt.*;
public class image extends JFrame{
Image img;
public image(){
Container con=getContentPane();
URL url=image.class.getResource("E:\\img\\2.JPEG");——这是图片的绝对路径
img=Toolkit.getDefaultToolkit().getImage(url);
setSize(200,200);
setLocation(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con.add(new CanvasPanel());
setVisible(true);
}
public static void main(String[] args) {
new image().setVisible(true);
}
class CanvasPanel extends Canvas{
public void paint(Graphics g){
super.paint(g);
Graphics2D g2=(Graphics2D)g;
g2.drawImage(img, 0, 0, this);
}
}
}
请高手帮我看看为什么图片显示不出来 展开
import javax.swing.*;
import java.awt.*;
public class image extends JFrame{
Image img;
public image(){
Container con=getContentPane();
URL url=image.class.getResource("E:\\img\\2.JPEG");——这是图片的绝对路径
img=Toolkit.getDefaultToolkit().getImage(url);
setSize(200,200);
setLocation(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con.add(new CanvasPanel());
setVisible(true);
}
public static void main(String[] args) {
new image().setVisible(true);
}
class CanvasPanel extends Canvas{
public void paint(Graphics g){
super.paint(g);
Graphics2D g2=(Graphics2D)g;
g2.drawImage(img, 0, 0, this);
}
}
}
请高手帮我看看为什么图片显示不出来 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询