Java: How to use image as background icon on JButton

Here I have shared a way to use image as background icon on JButton.

 

Code Example:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 

public class simpletest extends JFrame
{

String conversion;

  public simpletest()
  {
  setSize(300,200);
  setTitle(“Button Icon”);
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  
  ImageIcon iconA = new ImageIcon(“image002.jpg”);
  JPanel panel=new JPanel();
  getContentPane().add(panel);
  panel.setLayout(null);
  
  JButton beep;
  beep= new JButton(“Click”,iconA);
  beep.setRolloverIcon(iconA);
  beep.setBounds(100,60,120,50);
  
  panel.add(beep);
  
  }
          public static void main(String args[])
          {
          simpletest x=new simpletest();
          x.setVisible(true);
          }

}

OUTPUT:

button icon java

2 Responses to Java: How to use image as background icon on JButton

  1. blqj says:

    how come I cant do something like…

    ImageIcon iconA = new ImageIcon(“https://allaboutbasic.files.wordpress.com/2010/12/button-icon-java.png?w=302&h=201”);

    and get an image from the web?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s