java - How to add background to JPanel and then add a JButton on that JPanel -
//calling function imagepanel panel_2 = new imagepanel(new imageicon("c:/users/kagarwal/downloads/intacct_logo_standard_web.png").getimage()); panel_2.add(new jbutton()); panel_2.revalidate(); //called function public class imagepanel extends jpanel { private image img; public imagepanel(string img) { this(new imageicon(img).getimage()); } public imagepanel(image img) { this.img = img; dimension size = new dimension(img.getwidth(null), img.getheight(null)); setpreferredsize(size); setminimumsize(size); setmaximumsize(size); setsize(size); setlayout(null); } public void paintcomponent(graphics g) { g.drawimage(img, 0, 0, null); } }
requirement is: jpanel2 needs have background image, , on top of need add jbutton. but, issue here newly added jbutton not appears in given jpanel, shows background image. missing refresh ?
the problem in paintcomponent, ask graphics object draw image. should call superclass paintcomponent method invoking super.paintcomponent() passing graphics object, in order have components of panel correctly displayed.
Comments
Post a Comment