Java Swing Tutorials

Creating a Icon Button
previous Home Next

Some steps we need to follow they are given below:

Setting icon on the button: This program sets the icon on the button in Java Swing.

then we have some Code description, they are

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

 

Example

package r4r;
import javax.swing.*;
import java.awt.*;
public class createicononbutton {
public static void main(String[] args){
JFrame frame = new JFrame("Icon on button");
JButton button = new JButton("R4RTech Soft Solution");
Icon imgicon = new ImageIcon("icon_confused.gif");
JPanel panel = new JPanel();
button.setIcon(imgicon);
panel.add(button);
frame.add(panel, BorderLayout.NORTH);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
previous Home Next