将图像添加到JButton [英] Adding image to JButton

查看:114
本文介绍了将图像添加到JButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像添加到 JButton 。按钮的背景设置为黑色。我试图在它上面添加图像,但没有显示任何内容。背景颜色为黑色但图像缺失。

I want to add an image to a JButton. Background of the button is set to black. I tried to add the image on top of it, but nothing was shown. Background color was black but the image was missing.

public class Test extends JFrame {

    JButton b;
    JPanel p;

    Test() {
        p = new JPanel(new BorderLayout());
        b = new JButton();
        b.setBackground(Color.black);
        ImageIcon img = new ImageIcon("C:\\Users\\Aksi\\Documents\\NetBeansProjects\\test'\\src\\test\\Black_B.ico");
        b.setIcon(img);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(400, 400);
        p.add(b);
        add(p);
       validate();

   }
    public static void main(String args[]) throws IOException {
        Test ob = new Test();
        ob.setVisible(true);
    }
}


推荐答案

两个事情


  1. 路径看起来不对

  2. Java本身不支持 ico 格式

  1. The path looks wrong
  2. Java doesn't, natively, support the ico format

看一下路径,路径中有引号

Take a look at the path, there is quote mark in the path

C:\\Users\\Aksi\\Documents\\NetBeansProjects\\test'\\src\\test\\Black_B.ico

确定它假设在那里

这篇关于将图像添加到JButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆