大虾帮忙啊~~~~~~~~~~~~···

小猪头是猪 2009-11-06 12:14:24
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.*;

class m
{ public JPanel jp;
protected ImageIcon icon ;
public int width,height;

public m(String photoPath)
{

jp=new JPanel();
icon=new ImageIcon(photoPath);
width=icon.getIconWidth();
height=icon.getIconHeight();
jp.setSize(width,height);
}


JPanel lovd(){
return this.jp;
}

protected void paintComponent(Graphics g)
{
this.jp.paintComponents(g);
Image img=icon.getImage();
g.drawImage(img, 0, 0, this.jp);
}
}

public class test
{
private JFrame jf;

test(String Title)
{
jf=new JFrame(Title);

}
void set(){
m lo=new m("d://亲爱的1.jpg");
Dimension ScrSize=Toolkit.getDefaultToolkit().getScreenSize();
int x=(int)((ScrSize.width-500)/2);
int y=(int)((ScrSize.height-300)/2);
this.jf.setLayout(null);
this.jf.setBounds(0, 0, ScrSize.width, ScrSize.height);
this.jf.setResizable(false);
this.jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.jf.setVisible(true);
this.jf.add(lo.lovd());
}

public static void main(String [] args)
{

new test("test").set();
}


}
...全文
158 13 打赏 收藏 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuhua_sun 2009-11-06
  • 打赏
  • 举报
回复
嗯!我试试看啊!
小猪头是猪 2009-11-06
  • 打赏
  • 举报
回复
你把代码拷贝下来测试一下就知道了
没有报错 但是不能显示啊
小猪头是猪 2009-11-06
  • 打赏
  • 举报
回复
meiyou
liuhua_sun 2009-11-06
  • 打赏
  • 举报
回复
有报什么错吗?
小猪头是猪 2009-11-06
  • 打赏
  • 举报
回复
不能显示加载的图片啊
goodmrning 2009-11-06
  • 打赏
  • 举报
回复
什么问题啊?
bayougeng 2009-11-06
  • 打赏
  • 举报
回复

jp = new JPanel() {
public void paintComponent(Graphics g) {
paintComponents(g);
Image img = icon.getImage();
g.drawImage(img, 0, 0, this);
}
};

另外,m lo = new m("d://1.jpg");改成m lo = new m("d:\\1.jpg");
原来的那个paintComponent方法是没用的。去掉吧。
littlemonster 2009-11-06
  • 打赏
  • 举报
回复
当然不会显示你的图片了,因为你也没用你m类里的paintComponent方法呀,JPanel里也没加图。

感觉你的程序写的有点特别噢,

如果你是想
1 把一张图片放到JPanel上的话,不需要用drawImage呀,paint的,Graphics。。
2 如果你是想画一张图片放到JPanel上,那就画Graphics,也不需要你的d://亲爱的1.jpg




仅仅是想把一个image显示在Jpanel上吗?
很多例子的。
小猪头是猪 2009-11-06
  • 打赏
  • 举报
回复
我靠 就没有一个高手帮助小弟
smallbear923 2009-11-06
  • 打赏
  • 举报
回复
帮顶。。
dj_sunny1 2009-11-06
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 ybbps1109 的回复:]
this.jf.setVisible(true);
this.jf.add(lo.lovd());
这个反了吧?
[/Quote]

我刚试过,这两句位置换一下还是显示不出来。
YBBPS1109 2009-11-06
  • 打赏
  • 举报
回复
this.jf.setVisible(true);
this.jf.add(lo.lovd());
这个反了吧?
小猪头是猪 2009-11-06
  • 打赏
  • 举报
回复
那位大虾帮助啊哈

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧