javaGUI–icon

java
Author

dd21

Published

December 5, 2022

在maven项目中,将图片文件放入classes文件夹下才有效!!!!!

URL url = ImageIconDemo.class.getResource("csdn.png");获取不到图片的原因. 在这里插入图片描述 在这里插入图片描述 button.setToolTipText("图片按钮");// 鼠标悬停上面会提示的文字

package cn.usts.edu.lesson04;

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

public class ImageIconDemo extends JFrame {

    public ImageIconDemo() {
        JLabel label01 = new JLabel("图片icon");
        JButton button = new JButton();

        URL url = ImageIconDemo.class.getResource("csdn.png");// 获取图片位置
        ImageIcon imageIcon = new ImageIcon(url);// 实例化新的图片icon

        label01.setIcon(imageIcon);
        label01.setHorizontalAlignment(SwingConstants.CENTER);

        button.setIcon(imageIcon);
        button.setBounds(100,100,200,100);
        button.setToolTipText("图片按钮");// 鼠标悬停上面会提示的文字

        Container container = this.getContentPane();
        container.add(button);
        container.add(label01);

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,600,600);
        this.setVisible(true);
    }

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

解决方案 修改pom.xml文件

<!--所有文件复制到classes下-->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>
    </build>