Friday, 15 May 2015

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



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

this question has reply here:

background image jframe content 3 answers

i'm in sec term of computer engineering science .

my problem how add together jbutton etc... on background image ,you know have written below code , please help 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); seek { this.myimage=imageio.read(new file("d:\\1.jpg")); }//end seek catch(ioexception e) { joptionpane.showmessagedialog(null, "image dose not exist.","no image found",joptionpane.error_message); }//end grab 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 utilize layout manager when add together button doesn't display.

try using flowlayout on component.

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

you need implement getpreferredsize() component works.

java image swing background jframe

No comments:

Post a Comment