Two background images (container images) in Java swing)

Source: Internet
Author: User

You need to add images or background images to the container in Java swing.

Provides two simple solutions, one using jpanel and the other using jlabel.

1. jpanel (source code)

Package Oo; import Java. AWT. graphics; import Java. AWT. image; import Java. io. file; import javax. swing. imageicon; import javax. swing. jframe; import javax. swing. jpanel; public class drawing {jframe = new jframe (); public static jpanel gimage = NULL; public drawing () {initframe () ;}// initialization window public void initframe () {// use jpanel to add the background image gimage = new jpanel () {protected void paintcomponent (Graphics g) {imageicon icon = new imageicon ("image \ benbenla.jpg "); image IMG = icon. getimage (); G. drawimage (IMG, 0, 0, icon. geticonwidth (), icon. geticonheight (), icon. getimageobserver (); jframe. setsize (icon. geticonwidth (), icon. geticonheight () ;}}; jframe. settitle ("test background image"); jframe. add (gimage); jframe. pack (); jframe. setvisible (true); jframe. setdefaclocloseoperation (jframe. exit_on_close);} public static void main (string [] ARGs) {New Drawing ();}}

2. jlabel source code

Package swing. draw; import Java. AWT. image; import javax. swing. imageicon; import javax. swing. jframe; import javax. swing. jlabel;/** use jlabel to construct images */public class drawing2 {jlabel jlpic = new jlabel (); jframe = new jframe (); Public drawing2 () {init1frame ();} public void init1frame () {imageicon = new imageicon ("image \ benla.jpg"); icon. setimage (icon. getimage (). getscaledinstance (icon. geticonwidth (), icon. geticonheight (), image. scale_default); system. out. println (icon. geticonheight () + "" + icon. geticonwidth (); jlpic. setbounds (0, 0, 1366,768); jlpic. sethorizontalalignment (0); jlpic. seticon (icon); jframe. setsize (1366,768); jframe. add (jlpic); jframe. pack (); jframe. setdefaclocloseoperation (jframe. exit_on_close); jframe. setvisible (true);} public static void main (string ARGs []) {New drawing2 ();}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.