主要是获取图标然后把图标放到按钮上即可
一、新建一个普通的frame 窗口。
二、新建图片Icon将ImageIcon添加到button即可
代码
package GUI.Swing.按钮JButton;
import javax.swing.*;
import java.awt.*;
public class ButtonDemo extends JFrame {
public ButtonDemo() {
this.setVisible(true);
this.setBounds(100,100,400,300);
this.setTitle("ImageIcon按钮");
Container contentPane = this.getContentPane();
JButton button = new JButton("label");
String resource = "D:/Program Files/JetBrains/test1/Lab/src/GUI/Swing/IconAndImageLabel图片和图标标签/方糖黄.png";
ImageIcon imageIcon = new ImageIcon(resource);
button.setIcon(imageIcon);
contentPane.add(button);
}
public static void main(String[] args) {
new ButtonDemo();
}
}
问题,按钮无法正常显示的问题,可能 是因为图片的问题,大小不兼容,需要拉伸窗口解决。