【问题标题】:How do I add an image to a JButton如何将图像添加到 JButton
【发布时间】:2011-06-15 15:24:36
【问题描述】:

我正在尝试将图像添加到 JButton,但我不确定我缺少什么。当我运行以下代码时,按钮看起来与我创建它时没有任何图像属性完全相同。 Water.bmp 位于我的项目文件夹的根目录中。

ImageIcon water = new ImageIcon("water.bmp");
    JButton button = new JButton(water);
    frame.add(button);

【问题讨论】:

  • 应该工作...你可以尝试使用URL 形式的ImageIcon 构造函数,看看它有什么作用?可能是因为某种原因找不到图片文件。
  • 是的,它正在工作。代码没有变化。谢谢大家的建议。

标签: java swing icons jbutton embedded-resource


【解决方案1】:
public class ImageButton extends JButton {

    protected ImageButton(){
    }

    @Override
        public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        Image img = Toolkit.getDefaultToolkit().getImage("water.bmp");

        g2.drawImage(img, 45, 35, this);
        g2.finalize();
    }
}

或使用此代码

class MyButton extends JButton {

    Image image;
    ImageObserver imageObserver;


    MyButtonl(String filename) {
            super();
            ImageIcon icon = new ImageIcon(filename);
            image = icon.getImage();
            imageObserver = icon.getImageObserver();
        }

     public void paint( Graphics g ) {
            super.paint( g );
            g.drawImage(image,  0 , 0 , getWidth() , getHeight() , imageObserver);
        }
    }

【讨论】:

  • 太复杂了。有一个为按钮添加图标的内置机制,为什么会产生额外的问题?
  • @Rogach 不是专门针对这个问题,但在我的情况下,进一步控制图像在按钮中的放置位置很有用。
【解决方案2】:

我认为您的问题在于图像的位置。你应该把它放在你的源代码中,然后像这样使用它:

  JButton button = new JButton();
  try {
    Image img = ImageIO.read(getClass().getResource("resources/water.bmp"));
    button.setIcon(new ImageIcon(img));
  } catch (Exception ex) {
    System.out.println(ex);
  }

在本例中,假设图片位于 src/resources/ 文件夹中。

【讨论】:

  • 我不知道为什么,但这对我也不起作用。我什至构建了一个函数,它在目录中搜索想要的文件并找到它们 - 仍然没有图标。所以,我只使用以下行:button1.setIcon("path/pic.png"); - 这运行。任何想法为什么?
【解决方案3】:

这看起来像是位置问题,因为该代码非常适合添加图标。

由于我不知道你的文件夹结构,我建议添加一个简单的检查:

File imageCheck = new File("water.bmp");

if(imageCheck.exists()) 
    System.out.println("Image file found!")
else 
    System.out.println("Image file not found!");

这样,如果您的路径名错误,它会告诉您,而不是什么都不显示。如果文件不存在,应该抛出异常。

【讨论】:

    【解决方案4】:

    我只做了一件事,它对我有用..检查你的代码是否有这种方法..

    setResizable(false);
    

    如果它是假的,让它成为真的,它会工作得很好.. 我希望它有所帮助..

    【讨论】:

    • 框架 (?) 是否可调整大小无关
    • 是的,你也许是对的......但是当你有一个 Borderlayout 然后你做出这个声明时......你限制了框架......因此,如果你做到了,它会给你的框架空间。
    【解决方案5】:

    @罗加奇

    您可能想添加:

    // to remote the spacing between the image and button's borders
    button.setMargin(new Insets(0, 0, 0, 0));
    // to add a different background
    button.setBackground( ... );
    // to remove the border
    button.setBorder(null);
    

    【讨论】:

      【解决方案6】:
      buttonB.setIcon(new ImageIcon(this.getClass().getResource("imagename")));
      

      【讨论】:

        【解决方案7】:
        //paste required image on C disk
        JButton button = new JButton(new ImageIcon("C:water.bmp");
        

        【讨论】:

          【解决方案8】:

          这段代码对我有用:

              BufferedImage image = null;
              try {
                  URL file = getClass().getResource("water.bmp");
                  image = ImageIO.read(file);
              } catch (IOException ioex) {
                  System.err.println("load error: " + ioex.getMessage());
              }
              ImageIcon icon = new ImageIcon(image);
              JButton quitButton = new JButton(icon);
          

          【讨论】:

            【解决方案9】:

            例如,如果您在文件夹res/image.png 中有图像,您可以这样写:

            try
            {
                ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
                InputStream input = classLoader.getResourceAsStream("image.png");
                // URL input = classLoader.getResource("image.png"); // <-- You can use URL class too.
                BufferedImage image = ImageIO.read(input);
            
                button.setIcon(new ImageIcon(image));
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
            

            一行:

            try
            {
                button.setIcon(new ImageIcon(ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("image.png"))));
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
            

            如果图片大于按钮,则不会显示。

            【讨论】:

              【解决方案10】:

              您将图像放在资源文件夹中并使用以下代码:

              JButton btn = new JButton("");
              btn.setIcon(new ImageIcon(Class.class.getResource("/resources/img.png")));
              

              【讨论】:

                猜你喜欢
                • 2012-08-15
                • 2012-11-02
                • 1970-01-01
                • 1970-01-01
                • 2017-07-23
                • 1970-01-01
                • 1970-01-01
                • 2014-02-07
                相关资源
                最近更新 更多