Java Swing 标签 Label ,图标标签和图片标签。

一、初始化一个常规的JFrame

package GUI.Swing.IconAndImageLabel图片和图标标签;

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

public class LabelDemo extends JFrame {
    public LabelDemo() {
        //use the constructor to unit a Frame
        this.setVisible(true);
        this.setBounds(100,100,400,200);
        Container contentPane = this.getContentPane();
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        
    }

    public static void main(String[] args) {
        new LabelDemo();
    }
}

二、图片标签.

1. 得到 图片的url ,(使用相对的路径 失败,这里使用绝对路径)
2.将 new ImageIcon(url),使用url 创建图片.
3.创建标签,并且导入图片,(此处有2种方法)

1.第一种是在创建的时候传入3个参数,“name”,Icon,CENTER(位置) 即可
2.第二种是在创建后
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);

4.将标签放入容器中.
package GUI.Swing.IconAndImageLabel图片和图标标签;

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

public class LabelDemo extends JFrame {
    public LabelDemo() {
        //use the constructor to unit a Frame
        this.setVisible(true);
        this.setBounds(100, 100, 400, 200);
        Container contentPane = this.getContentPane();
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        //get url 使用 当前类名.class.getResource("");传入相对地址得到绝对地址.//无法使用,此处使用绝对路径
        String url = "D:/Program Files/JetBrains/test1/Lab/src/GUI/Swing/IconAndImageLabel图片和图标标签/方糖黄.png";
        //使用url得到一个 Image 的对象
        ImageIcon imageIcon = new ImageIcon(url);
        //创建一个label 并将url参数传递给label,并居中显示,查看源码可以得出有3个参数,String Icon 和对齐方式
        JLabel label = new JLabel("方糖黄.png", imageIcon, SwingConstants.CENTER);
        //add label to contentPane
        contentPane.add(label);
    }

    public static void main(String[] args) {
        new LabelDemo();
    }
}

效果

在这里插入图片描述

三、图标标签(就是自己使用画一个图标,不常用)

1.实现Icon 的接口并重写Override方法,得到宽和高
2.paintIcon ()可以绘画处icon,这里出现未知错误.
3.将绘制的图像 new 此类就可以的得到Icon 的对象
4. 创建Label的时候添加icon 对象到label里

代码:

package GUI.Swing.IconAndImageLabel图片和图标标签;

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

public class IconLabel extends JFrame implements Icon {
    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.fillRect(x,y,20,20);
    }

    @Override
    public int getIconWidth() {
        return 0;
    }

    @Override
    public int getIconHeight() {
        return 0;
    }
    //使用构造方法创建Frame

    public IconLabel() throws HeadlessException {
        this.setVisible(true);
        this.setBounds(100,100,400,400);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        //add Icon to label,注意paintIcon()在new  的时候就会初始化
        IconLabel iconLabel = new IconLabel();
        JLabel label = new JLabel("图标label",iconLabel,SwingConstants.CENTER);
        //add label to contentPane
        Container contentPane = this.getContentPane();
        contentPane.add(label);
        label.setHorizontalAlignment(SwingConstants.CENTER);
    }

    public static void main(String[] args) {
        new IconLabel();
    }
}

窗口闪烁,添加失败,位置错误。

发布了56 篇原创文章 · 获赞 2 · 访问量 467

猜你喜欢

转载自blog.csdn.net/jarvan5/article/details/105644018
今日推荐