swing - I need to set a background image for a java DesktopApplication -


i didn't know how, , there no background image property. researched answer find set labels icon inside panel null layout. worked , image there, covering single text field. can change z value of label? not see 'move back' option, if there one.

this should solve problem:

import java.awt.dimension; import java.awt.graphics; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception;  import javax.imageio.imageio; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.swingutilities;  public class testimage {      public static void main(string[] args) {         swingutilities.invokelater(new runnable() {              @override             public void run() {                 contentpane panel = new contentpane();                 jframe frame = new jframe();                 frame.setdefaultcloseoperation(jframe.dispose_on_close);                 frame.add(panel);                 frame.pack();                 frame.setvisible(true);             }         });     }      private static class contentpane extends jpanel {         bufferedimage image = null;          public contentpane() {             try {                 string pathtoimage = "c:/users/branislav/pictures/sun.jpg";                 image = imageio.read(new file(pathtoimage));             } catch (ioexception e) {                 e.printstacktrace();             }         }          protected void paintcomponent(graphics g) {             super.paintcomponent(g);             g.drawimage(image, 0, 0, null);         };          @override         public dimension getpreferredsize() {             return new dimension(image.getwidth(), image.getheight());         }     } } 

basically, set image on jpanel (contentpane). also, size of jpanel depends on size of image (at least in case).

regards.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -