您的位置:程序门 -> java -> j2se / 基础类



why i can"t adding a window to a container


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


why i can't adding a window to a container
发表于:2007-07-11 02:16:26 楼主
源程序如下:

import   java.awt.*;
import   java.awt.event.*;
import   javax.swing.*;

public   class   comcountry   extends   jframe   implements   itemlistener
{
private   string[]   flagtitle   =   { "canada ",   "china ",   "denmark ",
        "france ",   "germany ",   "india ",   "norway ",   "united   kingdom ",
        "united   states   of   america "};
private   imageicon   flagimage[]=   new   imageicon[9];
private   string   flagdescription[]=new   string[9];

private   jcombobox   countrynamecombobox=new   jcombobox();
private   descriptionpanel   descriptionpanel=new   descriptionpanel();

public   comcountry()
{
        flagimage[0]   =   new   imageicon( "images/ca.gif ");
                flagimage[1]   =   new   imageicon( "images/china.gif ");
                flagimage[2]   =   new   imageicon( "images/denmark.gif ");
                flagimage[3]   =   new   imageicon( "images/fr.gif ");
                flagimage[4]   =   new   imageicon( "images/germany.gif ");
                flagimage[5]   =   new   imageicon( "images/india.gif ");
                flagimage[6]   =   new   imageicon( "images/norway.gif ");
                flagimage[7]   =   new   imageicon( "images/uk.gif ");
                flagimage[8]   =   new   imageicon( "images/us.gif ");

                //   set   text   description
                flagdescription[0]   =   "the   maple   leaf   flag   \n\n "   +
                "the   canadian   national   flag   was   adopted   by   the   canadian   "   +
                "parliament   on   october   22,   1964   and   was   proclaimed   into   law   "   +
                "by   her   majesty   queen   elizabeth   ii   (the   queen   of   canada)   on   "   +
                "february   15,   1965.   the   canadian   flag   (colloquially   known   "   +
                "as   the   maple   leaf   flag)   is   a   red   flag   of   the   proportions   "   +
                "two   by   length   and   one   by   width,   containing   in   its   centre   a   "   +
                "white   square,   with   a   single   red   stylized   eleven-point   "   +
                "mapleleaf   centred   in   the   white   square. ";
                flagdescription[1]   =   "description   for   china   ...   ";
                flagdescription[2]   =   "description   for   denmark   ...   ";
                flagdescription[3]   =   "description   for   france   ...   ";
                flagdescription[4]   =   "description   for   germany   ...   ";
                flagdescription[5]   =   "description   for   india   ...   ";
                flagdescription[6]   =   "description   for   norway   ...   ";
                flagdescription[7]   =   "description   for   uk   ...   ";
                flagdescription[8]   =   "description   for   us   ...   ";
               
jlabel   titlelabel=new   jlabel( "qzm 's   first   program ");
jpanel   p1=new   jpanel();
p1.setlayout(new   borderlayout());
p1.add(titlelabel,borderlayout.center);
p1.add(countrynamecombobox,borderlayout.west);

getcontentpane().setlayout(new   borderlayout());
getcontentpane().add(p1,borderlayout.north);
getcontentpane().add(descriptionpanel,borderlayout.center);

countrynamecombobox.additemlistener(this);
}

public   static   void   main(string   []   args)
{
//   descriptionpanel   frame1=new   descriptionpanel();
comcountry   frame1=new   comcountry();
frame1.setvisible(true);
frame1.pack();
frame1.setdefaultcloseoperation(jframe.exit_on_close);
}

public   void   itemstatechanged(itemevent   e)
{
displaydescriptionpanel(countrynamecombobox.getselectedindex());
}

public   void   displaydescriptionpanel(int   index)
{
descriptionpanel.setimage(flagimage[index]);
descriptionpanel.settitle(flagtitle[index]);
descriptionpanel.settext(flagdescription[index]);
}
}

class   descriptionpanel   extends   jframe
{
private   jtextarea   textarea1=new   jtextarea();
private   jlabel   countryimagelabel=new   jlabel();
private   jlabel   countrytitlelabel=new   jlabel();  
public   descriptionpanel()
{
jpanel   p1=new   jpanel();

p1.setlayout(new   borderlayout());
p1.add(countryimagelabel,borderlayout.center);
p1.add(countryimagelabel,borderlayout.south);

jscrollpane   jscrollpane=new   jscrollpane();
jscrollpane.add(textarea1);
textarea1.setlinewrap(true);
textarea1.setwrapstyleword(true);

setlayout(new   borderlayout());
add(jscrollpane,borderlayout.east);
add(p1,borderlayout.center);
}

public   void   setimage(imageicon   icon)
{
countryimagelabel.seticon(icon);
dimension   dimension=new   dimension(icon.geticonwidth(),icon.geticonheight());
countryimagelabel.setpreferredsize(dimension);  
}

public   void   settitle(string   title)
{
countrytitlelabel.settext(title);
}

public   void   settext(string   text)
{
textarea1.settext(text);
}
}

编译   ok
运行出现如下错误。
exception   in   thread   "main "   java.lang.illegalargumentexception:   adding   a   window   to   a   container
                at   java.awt.container.addimpl(container.java:1022)
                at   java.awt.container.add(container.java:903)
                at   comcountry. <init> (comcountry.java:55)
                at   comcountry.main(comcountry.java:62)
本人系java初学者,望高手指点。不甚感激。
发表于:2007-07-11 09:27:001楼 得分:0
代码贴的太多,没看完。
发表于:2007-07-11 09:51:462楼 得分:0
应该是这个的
class   descriptionpanel   extends   jframe{}
这里的
private   descriptionpanel   descriptionpanel=new   descriptionpanel();
生成的descriptionpanel   是一个窗口对象。
不能再向comcountry中增加相同等级的对象了。
可以把class   descriptionpanel   extends   jframe{}
改为一个轻量级的容器。比如说
改为class   descriptionpanel   extends   jpanel{}


快速检索

最新资讯
热门点击