Home  >  Article  >  Java  >  How to use jlabel in JAVA

How to use jlabel in JAVA

(*-*)浩
(*-*)浩Original
2019-05-28 11:09:3923846browse

JLabel objects can display text, images, or both. You can specify where the label content is aligned in the label display area by setting the vertical and horizontal alignment. By default, labels are centered vertically within their display area. By default, text-only labels are aligned at the start edge; image-only labels are aligned horizontally and center.

How to use jlabel in JAVA

You can also specify the position of the text relative to the image. By default, the text is positioned at the end edge of the image, and both text and image are aligned vertically.

Determine the starting edge and ending edge of the label based on the value of its ComponentOrientation property. Currently, the default ComponentOrientation settings map the starting edge to the left and the ending edge to the right.

Construction method summary:

JLabel()
         // 创建无图像并且其标题为空字符串的 JLabel。
JLabel(Icon image)
         // 创建具有指定图像的 JLabel 实例。
JLabel(Icon image, int horizontalAlignment)
          //创建具有指定图像和水平对齐方式的 JLabel 实例。
JLabel(String text)
         // 创建具有指定文本的 JLabel 实例。
JLabel(String text, Icon icon, int horizontalAlignment)
          //创建具有指定文本、图像和水平对齐方式的 JLabel 实例。
JLabel(String text, int horizontalAlignment)
          //创建具有指定文本和水平对齐方式的 JLabel 实例。

Common methods of JLabel:

// 设置 文本 和 图片
void setText(String text)
void setIcon(Icon icon)

// 设置文本相对于图片的位置(文本默认在图片右边垂直居中)
void setHorizontalTextPosition(int textPosition)
void setVerticalTextPosition(int textPosition)

// 设置标签内容(在标签内)的对其方式(默认左对齐并垂直居中)
void setHorizontalAlignment(int alignment)
void setVerticalAlignment(int alignment)

// 设置文本的字体类型、样式 和 大小
void setFont(Font font)

Code examples:

package com.xiets.swing;

import javax.swing.*;
import java.awt.*;

public class Main {

    public static void main(String[] args) {
        JFrame jf = new JFrame("测试窗口");
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // 创建内容面板,默认使用流式布局
        JPanel panel = new JPanel();

        /*
         * 只显示文本
         */
        JLabel label01 = new JLabel();
        label01.setText("Only Text");
        label01.setFont(new Font(null, Font.PLAIN, 25));  // 设置字体,null 表示使用默认字体
        panel.add(label01);

        /*
         * 只显示图片
         */
        JLabel label02 = new JLabel();
        label02.setIcon(new ImageIcon("demo01.jpg"));
        panel.add(label02);

        /*
         * 同时显示文本和图片
         */
        JLabel label03 = new JLabel();
        label03.setText("文本和图片");
        label03.setIcon(new ImageIcon("demo02.jpg"));
        label03.setHorizontalTextPosition(SwingConstants.CENTER);   // 水平方向文本在图片中心
        label03.setVerticalTextPosition(SwingConstants.BOTTOM);     // 垂直方向文本在图片下方
        panel.add(label03);

        jf.setContentPane(panel);
        jf.pack();
        jf.setLocationRelativeTo(null);
        jf.setVisible(true);
    }

}

The above is the detailed content of How to use jlabel in JAVA. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn