java - Simpliest way to make a custom JPanel background -


i've got problem jpanel background. want put image "aaa.jpg" in background of panel "p5". if done without creating new class. simple way set image on background of panel? here's part of code:

glowny4.setlayout(new gridlayout(1,2));     glowny4.add (p5);     glowny4.add (p6);   

there 2 main ways know of make image show in background of jpanel:

  • use jlabel instead of jpanel,
    • simply make image imageicon
    • pass jlabel via seticon(myicon) method.
    • make sure give jlabel decent layout manager though.
    • this easiest way implement this, requires image can't scale different size if need be.
  • use jpanel , overwrite paintcomponent(...) method.
    • be sure call super method first inside of override.
    • then call g.drawimage(...) passing in bufferedimage method.
    • this works when image needs scale different size.

please search site has been posted on before sample code, of mine.


Comments

Popular posts from this blog

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -