Swing

Setting icon on the button in Java
Previous Home Next

 

        
In this page of the example we are going to explain how to show the icon on the button in Java Swing.
This program sets the icon on the button in Java Swing. 

 
setIcon(Icon):
This method is used to set the specified Icon on the button.
ImagIcon(String image_name):
This method is used to creates an object of image.

 

package r4r;
import javax.swing.*;
import java.awt.*;

public class createicononbutton {
	public static void main(String[] args){
		  JFrame f = new JFrame("Icon on button");
		  JButton btn = new JButton("R4RTech Soft Solution");
		  Icon imgicon = new ImageIcon("icon1.gif");
		  JPanel panel = new JPanel();
		  btn.setIcon(imgicon);
		  panel.add(btn);
		  f.add(panel, BorderLayout.NORTH);
		  f.setSize(400, 400);
		  f.setVisible(true);
		  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		  }
}

Previous Home Next