Java Swing Interface Programming (5) --- set images in JLabel

Source: Internet
Author: User

Package com. beyole. util; import java. awt. color; import java. io. file; import javax. swing. icon; import javax. swing. imageIcon; import javax. swing. JFrame; import javax. swing. JLabel; public class test4 {public static void main (String [] args) {JFrame frame = new JFrame ("Crystal"); String path = "f:" + File. separator + "1.jpg"; Icon icon = new ImageIcon (path); JLabel label = null; label = new JLabel (icon, JLabel. CENTER); label. setForeground (Color. RED); label. setBackground (Color. YELLOW); frame. add (label); frame. setSize (300,160); frame. setBackground (Color. white); frame. setLocation (300,200); frame. setVisible (true );}}

This instance does not use the input stream to adjust the image, because I think it is too troublesome. It is estimated that it will not be used in future projects. Of course, it is just my personal opinion.

The code for calling the image is only Icon icon = new ImageIcon (path); then you can add the icon to the JLabel constructor.

Program:

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.