Java Basic Problems

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 thoughts on “Java: How to use image as background icon on JButton”

  1. 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?

    Liked by 1 person

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 )

Facebook photo

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

Connecting to %s