Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the javax.swing.JLabel tag

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to use javax.swing.JLabel tags for you. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1. Overview

Official JavaDocsApi: javax.swing.JLabel

JLabel, label. Tags are mainly used to display text or pictures, and can also display text and pictures at the same time.

The construction method of JLabel:

/ * * meaning of the parameters in the constructor: * text: text displayed by the tag * image: picture displayed by the tag * horizontalAlignment: the horizontal orientation of the tag content (within the tag) is centered by default, which can be set by method) * / JLabel () JLabel (String text) JLabel (String text, int horizontalAlignment) JLabel (Icon image) JLabel (Icon image, int horizontalAlignment) JLabel (String text, Icon image, int horizontalAlignment)

Common methods of JLabel:

/ / sets the position of the text and picture void setText (String text) void setIcon (Icon icon) / / sets the position of the text relative to the picture (the text is vertically centered on the right side of the picture by default) void setHorizontalTextPosition (int textPosition) void setVerticalTextPosition (int textPosition) / / sets the way the tag content (within the tag) is aligned (left by default and centered vertically) void setHorizontalAlignment (int alignment) void setVerticalAlignment (int alignment) / / sets the picture and text The gap between text void setIconTextGap (int iconTextGap) / * * the following method defines the font type of text in the JComponent base class * / Style and size void setFont (Font font) / / set font color void setForeground (Color fg) / / prompt text displayed when the mouse moves over the component void setToolTipText (String text) / / sets the component's background void setBackground (Color bg) / / sets whether the component is visible void setVisible (boolean visible) / / sets whether the component is opaque JLabel defaults to transparent. Only when it is set to opaque can the background void setOpaque (boolean isOpaque) / / set the preferred size of the component void setPreferredSize (Dimension preferredSize) / / set the minimum size of the component void setMinimumSize (Dimension minimumSize) / / set the maximum size of the component void setMaximumSize (Dimension maximumSize) / * * the alignment parameters above refer to DocsApi for specific values * setXXX (...) Methods often correspond to the getXXX () method. , /

When the picture is displayed, the creation of the Icon instance (generally, the ImageIcon,ImageIcon is created to implement Icon):

The construction method of ImageIcon:

/ / use local path picture to create ImageIconImageIcon (String filename) / / use network path picture to create ImageIconImageIcon (URL location) / / use byte array of picture file to create ImageIconImageIcon (byte [] imageData) / / use java.awt.Image instance to create ImageIconImageIcon (java.awt.Image image)

Creation of java.awt.Image instance:

/ / method 1: read local, network or memory images through the java.awt.Toolkit utility class (supports GIF, JPEG or PNG) Image image = Toolkit.getDefaultToolkit () .getImage (String filename); Image image = Toolkit.getDefaultToolkit () .getImage (URL url); Image image = Toolkit.getDefaultToolkit () .createImage (byte [] imageData) / / method 2: read pictures in local, network or memory through the javax.imageio.ImageIO tool class (BufferedImage inherits from Image) BufferedImage bufImage = ImageIO.read (File input); BufferedImage bufImage = ImageIO.read (URL input); BufferedImage bufImage = ImageIO.read (InputStream input); / * * PS_01: picture width and height: BufferedImage can directly obtain the width and height of pictures through bufImage.getWidth () and bufImage.getHeight () methods * another ImageObserver parameter is required for Image to obtain width and height. * * PS_02: image clipping: BufferedImage can intercept any part of the image through bufImage.getSubimage (int x, int y, int w, int h) and return a new BufferedImage instance. * * PS_03: image zooming: Image can * zoom the image through the image.getScaledInstance (int width, int height, int hints) method to return a new Image instance. , /

two。 Code example

Package com.xiets.swing;import javax.swing.*;import java.awt.*;public class Main {public static void main (String [] args) {JFrame jf = new JFrame ("Test window"); jf.setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE); / / create a content panel, using streaming layout JPanel panel = new JPanel () by default; / * display only text * / JLabel label01 = new JLabel (); label01.setText ("Only Text") Label01.setFont (new Font (null, Font.PLAIN, 25)); / / set the font. Null means to use the default font panel.add (label01); / * * display only pictures * / JLabel label02 = new JLabel (); label02.setIcon (new ImageIcon ("demo01.jpg")); panel.add (label02); / * * display both text and pictures * / JLabel label03 = new JLabel (); label03.setText ("text and pictures") Label03.setIcon (new ImageIcon ("demo02.jpg")); label03.setHorizontalTextPosition (SwingConstants.CENTER); / / horizontal text in the center of the picture label03.setVerticalTextPosition (SwingConstants.BOTTOM); / / Vertical text below the picture panel.add (label03); jf.setContentPane (panel); jf.pack (); jf.setLocationRelativeTo (null); jf.setVisible (true);}}

This is the end of this article on "how to use javax.swing.JLabel tags". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report