主要是獲取圖標然後把圖標放到按鈕上即可
一、新建一個普通的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();
}
}
問題,按鈕無法正常顯示的問題,可能 是因爲圖片的問題,大小不兼容,需要拉伸窗口解決。