Jlabel for Swing Development

Source: Internet
Author: User

The jlabel object can display text, images, or both. You can set the vertical and horizontal alignment modes to specify where the label content is aligned in the tag display area. By default, labels are vertically centered and aligned in the display area. By default, labels that only show text are the starting edge alignment, while labels that only show images are horizontally centered and alignment.

You can also specify the position of the text relative to the image. By default, text is located at the end of the image, and the text and image are vertically aligned.
Constructor:
Jlabel ()
Create a jlabel with no image and its title is an empty string.
Jlabel (icon image)
Create a jlabel instance with the specified image.
Jlabel (icon image, int horizontalalignment)
Create a jlabel instance with the specified image and horizontal alignment.
Jlabel (string text)
Creates a jlabel instance with the specified text.
Jlabel (string text, icon, int horizontalalignment)
Create a jlabel instance with the specified text, image, and horizontal alignment.
Jlabel (string text, int horizontalalignment)
Create a jlabel instance with the specified text and horizontal alignment.

Common Methods:
Gethorizontalalignment ()
Returns the alignment of the label content along the X axis.
Gethorizontaltextposition ()
Returns the horizontal position of the text of the label relative to the image.
GetIcon ()
Returns the graphical image (font, icon) displayed by this label ).
Gettext ()
Returns the text string displayed for the tag.
Sethorizontalalignment (INT alignment)
Sets the alignment of TAG content along the X axis.
Sethorizontaltextposition (INT textposition)
Set the horizontal position of the label text relative to the image.
Seticon (icon)
Define the icon to be displayed for this component.
Settext (string text)
Defines the single line of text that this component will display.
Setui (labelui UI)
Sets the L & F object for rendering this component.
Setverticalalignment (INT alignment)
Sets the alignment of TAG content along the Y axis.
Setverticaltextposition (INT textposition)
Set the vertical position of the text of the label relative to the image.

Example 1: add images and text in jlabel

import java.awt.FlowLayout; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingConstants; public class MixingIconLabel {   public static void main(String[] args) {     JFrame.setDefaultLookAndFeelDecorated(true);     JFrame frame = new JFrame();     frame.setTitle("JLabel Test");     frame.setLayout(new FlowLayout());     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     ImageIcon imageIcon = new ImageIcon("yourFile.gif");     JLabel label = new JLabel("Mixed", imageIcon, SwingConstants.RIGHT);     frame.add(label);     frame.pack();     frame.setVisible(true);   } } 

Example 2: Add HTML text to jlabel

import javax.swing.JFrame; import javax.swing.JLabel; public class HTMLLabel {   public static void main(String[] a) {     JFrame frame = new JFrame();     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     JLabel label = new JLabel("

Example 3: rewrite jlabel

import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.Serializable; import javax.swing.JLabel; public class Colors extends JLabel implements Serializable {   transient private Color color; // not persistent   private boolean rectangular; // is persistent   public Colors() {     addMouseListener(new MouseAdapter() {       public void mousePressed(MouseEvent me) {         change();       }     });     rectangular = false;     setSize(200, 100);     change();   }   public boolean getRectangular() {     return rectangular;   }   public void setRectangular(boolean flag) {     this.rectangular = flag;     repaint();   }   public void change() {     color = randomColor();     repaint();   }   private Color randomColor() {     int r = (int) (255 * Math.random());     int g = (int) (255 * Math.random());     int b = (int) (255 * Math.random());     return new Color(r, g, b);   }   public void paint(Graphics g) {     Dimension d = getSize();     int h = d.height;     int w = d.width;     g.setColor(color);     if (rectangular) {       g.fillRect(0, 0, w - 1, h - 1);     } else {       g.fillOval(0, 0, w - 1, h - 1);     }   } } 

Example 4: Add jlabel to jscrollpane to facilitate the display of large images

Import javax. swing. imageicon; import javax. swing. jframe; import javax. swing. jlabel; import javax. swing. jscrollpane; public class scrollpaneframe {public static void main (string [] ARGs) {jframe frame = new jframe (); jlabel image = new jlabel (New imageicon ("a.jpg ")); frame. getcontentpane (). add (New jscrollpane (image); frame. setsize (300,300); frame. setdefaclocloseoperation (jframe. exit_on_close); frame. setvisible (true) ;}}example 5: added the Unicode code import Java. AWT. gridlayout; import javax. swing. jframe; import javax. swing. jlabel; public class Unicode {public static void main (string ARGs []) {unicodejframe = new unicodejframe (); unicodejframe. setdefaclocloseoperation (jframe. exit_on_close); unicodejframe. setsize (350,250); unicodejframe. setvisible (true) ;}} class unicodejframe extends jframe {public unicodejframe () {super ("demonstrating Unicode"); setlayout (New gridlayout (8, 1 )); jlabel englishjlabel = new jlabel ("/u0057/u0065/u006c/u0063" + "/logs/u006d/u0065/u0020/u0074/logs/u0020unicode/u0021"); englishjlabel. settooltiptext ("this is English"); add (englishjlabel ); jlabel chinesejlabel = new jlabel ("/u6b22/u8fce/u4f7f/u7528" + "/u0020/u0020unicode/u0021"); chinesejlabel. settooltiptext ("this is traditional Chinese"); add (chinesejlabel ); jlabel labels = new jlabel ("/u0414/u043e/u0431/u0440" + "/u043e/u0020/u043f/queues/u0436/u0430/u043b/u043e/u0432" + "/ u0430/u0422/u044a/u0020/u0432/u0020unicode/u0021 "); cyrillicjlabel. settooltiptext ("This is Russian"); add (cyrillicjlabel ); jlabel plaintext = new jlabel ("/u0042/u0069/u0065/u006e/u0076" + "/u0065/fingerprint/u0075/u0065/u0020/u0061/u0075/u0020unicode/u0021 "); frenchjlabel. settooltiptext ("This is French"); add (frenchjlabel ); jlabel germanjlabel = new jlabel ("/u0057/u0069/latest/u006b/latest" + "/u006d/u006d/u0065/latest/u0020/u007a/u0075/u0020unicode/u0021 "); germanjlabel. settooltiptext ("This is German"); add (germanjlabel); jlabel japanesejlabel = new jlabel ("Unicode/u3078/u3087/u3045" + "/u3053/u305d/u0021 "); japanesejlabel. settooltiptext ("this is Japanese"); add (japanesejlabel ); jlabel labels = new jlabel ("/u0053/u00e9/u006a/u0061" + "/u0020/u0042/u0065/u006d/u0076/u0069/u006e/u0064/users/u0020 ""Unicode/u0021 "); portuguesejlabel. settooltiptext ("this is Portuguese"); add (portuguesejlabel ); jlabel labels = new jlabel ("/u0042/u0069/u0065/u006e" + "/u0076/u0065/u006e/u0069/u0064/u0061/u0020/u0061 Unicode/u0020" + /u0021 "); spanishjlabel. settooltiptext ("this is Spanish"); add (spanishjlabel );}}

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.