Java JButton 使用教程,Swing組件按鈕

主要是獲取圖標然後把圖標放到按鈕上即可

一、新建一個普通的frame 窗口。

二、新建圖片Icon將ImageIcon添加到button即可

三、方法和圖片label一樣圖片label教程

代碼

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");
        //add the picture to the button
        //use the relative path
        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();
    }
}

問題,按鈕無法正常顯示的問題,可能 是因爲圖片的問題,大小不兼容,需要拉伸窗口解決。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章