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();
    }
}

问题,按钮无法正常显示的问题,可能 是因为图片的问题,大小不兼容,需要拉伸窗口解决。

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

猜你喜欢

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