曾经有个网友是这样教我的 new一个jpanel 向这个组件添加图片 在把这个组件add 到 frame中 就可以了 我试过 能行 我把我的那个程序代码贴出来 lz看看吧 应该可以看懂
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class windowbox extends frame implements actionlistener {
box basebox, boxv1, boxv2;
textfield text2;
button button1, button2;
panel panel1;
choice choice;
jpanel imagepanel;
windowbox() {
imagepanel = new imagejpanel(new imageicon(windowbox.class.getresource("002.jpg")));
choice = new choice();
choice.add("信息工程系");
choice.add("软件学院");
choice.add("自动化系");
choice.add("英语系");
choice.add("会计系");
panel1 = new panel();
button1 = new button("确定");
button2 = new button("取消");
text2 = new textfield(12);
panel1.add(button1);
panel1.add(button2);
// this.setbackground(color.light_gray);//设置背景颜色
button1.addactionlistener(this);
button2.addactionlistener(this);
boxv1 = box.createverticalbox();
boxv1.add(new label("所在院系"));
boxv1.add(box.createverticalstrut(8));
boxv1.add(new label("姓名"));
boxv2 = box.createverticalbox();
boxv2.add(choice);
boxv2.add(box.createverticalstrut(8));
boxv2.add(text2);
basebox = box.createhorizontalbox();
basebox.add(boxv1);
basebox.add(box.createhorizontalstrut(10));
basebox.add(boxv2);
imagepanel.setlayout(new flowlayout());//setlayout(new flowlayout());
imagepanel.setsize(250, 150);//新增的
imagepanel.add(basebox);//add(basebox);
imagepanel.add(panel1);//add(panel1);
add(imagepanel);//新增的
setbounds(120, 125, 250, 150);
addwindowlistener(new windowadapter()
{
public void windowclosing(windowevent e)
{system.exit(0);
}
}
);
setvisible(true);
validate();
}
public void actionperformed(actionevent e) {
if (e.getsource() == button1) {
new xinxi();
} else {
text2.settext(null);
}
}
class imagejpanel extends jpanel {
image image;
imageicon icon;
public imagejpanel(imageicon icon) {
this.setopaque(false);//新增的
//imageicon icon; //去掉了. 因为参数就有icon了
image img;
// imagejpanel()//错误语句
{
//icon = new imageicon(windowbox.class.getresource ("uname.jpg"));//icon已经作为参数了.
img = icon.getimage();
this.image = img;//新增的
}
}
protected void paintcomponent(graphics g) {
g.drawimage(image, 0, 0, null); // 用g 把image画出来 // 这句修改了下.
super.paintcomponent(g);
}
}
}
class xinxi extends frame
{
box basebox,boxv1,boxv2;
textfield text1,text2,text3,text4;
textarea text5;
xinxi()
{
text1=new textfield(12);
text2=new textfield(12);
text3=new textfield(12);
text4=new textfield(12);
text5=new textarea(3,2);
boxv1=box.createverticalbox();
boxv1.add(new label("所属院系"));
boxv1.add(box.createverticalstrut(4));
boxv1.add(new label("姓名"));
boxv1.add(box.createverticalstrut(4));
boxv1.add(new label("班级"));
boxv1.add(box.createverticalstrut(4));
boxv1.add(new label("寝室号"));
boxv1.add(box.createverticalstrut(4));
boxv1.add(new label("寝室成员"));
boxv2=box.createverticalbox();
boxv2.add(text1);
//text1.addactionlistener(this);
boxv2.add(box.createverticalstrut(30));
boxv2.add(text2);
// text2.addactionlistener(this);
boxv2.add(box.createverticalstrut(30));
boxv2.add(text3);
//text3.addactionlistener(this);
boxv2.add(box.createverticalstrut(30));
boxv2.add(text4);
//text4.addactionlistener(this);
boxv2.add(box.createverticalstrut(30));
boxv2.add(text5);
//text5.addactionlistener(this);
basebox=box.createhorizontalbox();
basebox.add(boxv1);
basebox.add(box.createhorizontalstrut(10));
basebox.add(boxv2);
setlayout(new flowlayout());
add(basebox);
setbounds(120,125,350,350);
addwindowlistener(new windowadapter()
{
public void windowclosing(windowevent e)
{setvisible(false);
}
}
);
setvisible(true);
validate();
}
public void windowopened(windowevent e) {
}
}
public class chaxun {
public static void main(string args[]) {
new windowbox();
}
}