java - how to add a buffered image as back ground of JFrame then add a panel on this image? -


this question has answer here:

i'm in second term of computer engineering .

my problem how add jbutton etc... on background image ,you know have written below code , please me continue: said jbotton can't shown on image , here problem.

import java.awt.color; import java.awt.graphics; import java.awt.image; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception;  import javax.imageio.imageio; import javax.swing.jbutton; import javax.swing.jcomponent; import javax.swing.jframe; import javax.swing.joptionpane; import javax.swing.jpanel;  public class mycalcframe extends jframe {     private bufferedimage myimage;     private jpanel mypanel;     private jbutton mybtn;      public mycalcframe()     {         this.setbounds(410, 110, 600, 450);         this.setdefaultcloseoperation(jframe.exit_on_close);          this.setresizable(false);         this.setalwaysontop(true);            try         {             this.myimage=imageio.read(new file("d:\\1.jpg"));         }//end try         catch(ioexception e)         {             joptionpane.showmessagedialog(null, "image dose not exist.","no image found",joptionpane.error_message);         }//end catch         this.mypanel=new jpanel();         this.mypanel.setbackground(color.black);         this.setcontentpane(new imagepanel(myimage));         mybtn=new jbutton("hello");         this.getcontentpane().add(mybtn);          this.setvisible(true);     }//end mycalcframe constructor      class imagepanel extends jcomponent      {         private image image;          public imagepanel(image image)         {             this.image = image;         }//end constructor         @override         protected void paintcomponent(graphics g)         {             g.drawimage(image, 0, 0, null);         }//en paintcomponent     }//end imagepanel     //################ end constructor ########################     //public void paint(graphics g)     //{     //  g.drawimage(myimage, 0, 0, this);     //}//end method paint      //@@@@@@@@@@@@@@@@@@@@@@@@ main @@@@@@@@@@@@@@@@@@@@@@@@@@@@     public static void main(string[] args)     {         //jframe.setdefaultlookandfeeldecorated(true);         new mycalcframe();     }//end method main     //@@@@@@@@@@@@@@@@@@@@@@@@ main @@@@@@@@@@@@@@@@@@@@@@@@@@@@@ }//end class mycalcframe 

a jcomponent doesn't use layout manager when add button doesn't display.

try using flowlayout on component.

also, don't use setbounds() frame. should pack() , use setlocationbyplatform(true), frame displayed @ preferred size.

you need implement getpreferredsize() component works.


Comments