java标签组件命名_Java eleven 标签组件与图标

1、标签组件JLabel

构造方法

说明

JLabel()

不带图标和文本的标签

JLabel(Icon icon)

带图标

JLabel(Icon icon,int aligment)

带图标,水平对齐方式

JLabel(String text,int aligment)

带文本,水平对齐方式

JLabel(String text,Icon icon,int aligment)

带文本,图标,标签内容的水平对齐方式

2、图标ImageIcon

package Eleven;

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.SwingConstants;

import javax.swing.WindowConstants;

import java.awt.Container;

import java.net.URL;

public class MyImageIcon extends JFrame{

public MyImageIcon(){

Container container = getContentPane();

/*javax.swing.JLabel.JLabel(String text, int horizontalAlignment)

Creates a JLabel instance with the specified text and horizontal alignment.

The label is centered vertically in its display area.

Parameters:

text The text to be displayed by the label.

horizontalAlignment One of the following constants defined in SwingConstants: LEFT, CENTER, RIGHT, LEADING or TRAILING.*/

JLabel jLabel = new JLabel("带图标的标签组件",JLabel.CENTER);

//URL java.lang.Class.getResource(String name)

URL url = MyImageIcon.class.getResource("watermelon.jpg");

//javax.swing.ImageIcon.ImageIcon(URL location)

Icon icon = new ImageIcon(url);

/*void javax.swing.JLabel.setIcon(Icon icon)

Defines the icon this component will display.

If the value of icon is null, nothing is displayed.

The default value of this property is null.

This is a JavaBeans bound property.

Parameters:

icon */

jLabel.setIcon(icon);

jLabel.setHorizontalAlignment(SwingConstants.CENTER);

jLabel.setOpaque(true);//不透明

container.add(jLabel);

setVisible(true);

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

setSize(186,168);

}

public static void main(String[] args){

new MyImageIcon();

}

}

b719c775f6431ea13abbbed64d8e6c38.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值