您的位置:程序门 -> java -> j2se / 扩展类



在frame更改显示图片


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


在frame更改显示图片[无满意答案结贴,结贴人:fenglibing]
发表于:2007-10-12 11:19:28 楼主
我写了一个panel,用于显示图片,代码如下:
..
        public   picpanel(string   picpath)   {
                try   {
                        jbinit(picpath);
                }   catch   (exception   e)   {
                        e.printstacktrace();
                }
        }

        jlabel   jl   =   null;
        imageicon   img   =   null;

        private   void   jbinit(string   picpath)   throws   exception   {
                this.setlayout(null);
                setvisible(true);
                img   =   new   imageicon(picpath);
                jl   =   new   jlabel(img);
                this.setlayout(new   borderlayout());
                this.setsize(new   dimension(400,   359));
                this.add(jl,   borderlayout.center);

        }
..
我在frame中调用panel并把图片地址以参数发给panel:
picpanel   pp=new   picpanel("c:\\t.jpg");
panelcenter.add(pp,borderlayout.north);                
this.getcontentpane().add(   panelcenter,   borderlayout.center   );
可以正常显示图片,
但我希望在frame中加入一个按钮方法,点击该按钮来切换panel中的图片,我采用的是如下方法:
        private   void   button1_actionperformed(actionevent   e)   {
                pp=new   picpanel("c:\\t2.jpg");
                panelcenter.add(pp,borderlayout.west);
                this.getcontentpane().add(   panelcenter,   borderlayout.center   );
        }
不起作用。
请高手帮个忙,怎么样解决这个问题
发表于:2007-10-12 13:10:311楼 得分:0
你的事件处理怎么是button1_actionperformed,我一直都认为是actionperformed,这样写也行吗
你加validate方法看行不,它可以重新布置组件
发表于:2007-10-12 13:35:342楼 得分:0
panelcenter.add(pp,borderlayout.north);      
panelcenter.add(pp,borderlayout.west);   pp应该放同一位置
panelcenter.updateui();
发表于:2007-10-12 14:10:473楼 得分:0
帮顶

不会gui编程  

一般做web开发的
发表于:2007-10-12 14:17:304楼 得分:0
照楼上几位的方法,我把方法改成这样也不行:
        private   void   button1_actionperformed(actionevent   e)   {
                pp=new   picpanel("c:\\t2.jpg");
                panelcenter.validate();
                panelcenter.add(pp,borderlayout.north);
                panelcenter.updateui();
                //this.getcontentpane().add(   panelcenter,   borderlayout.center   );
        }
发表于:2007-10-12 14:58:135楼 得分:0
1.   add   event;
2.   repaint();
发表于:2007-10-12 16:54:016楼 得分:0
是这样写吗:
        private   void   button1_actionperformed(actionevent   e)   {
                pp=new   picpanel("c:\\t2.jpg");
                panelcenter.validate();
                panelcenter.add(pp,borderlayout.north);
                panelcenter.updateui();
                panelcenter.repaint();
                //this.getcontentpane().add(   panelcenter,   borderlayout.center   );
        }
不行的
请再帮忙
发表于:2007-10-15 10:43:537楼 得分:0
上去
发表于:2007-10-17 08:09:518楼 得分:0
这是一个很难的问题,还是一个不肖一顾的问题,
不管怎么样,还是请swing老大轻抬贵手,给解决解决,或者您有例子什么的
给我一个
发表于:2007-10-17 08:32:269楼 得分:0
将面板获取回来,并取得上面的jlable物件,然后重新设置图片即可,不需要重新new   picpanel(),既耗时间又费内存


快速检索

最新资讯
热门点击