Friday, September 26, 2008


Show Icon on The Button

With the help of java swing, you can add images, icons in the application to make your GUI application interface more attractive. With the help of this example, you can learn how to add image on the button or other control. This example expain you add image file. The code is given below :

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

public class IconButton{
public static void main(String[] args){
JFrame frame = new JFrame("Swing Icon Example");
JButton button = new JButton("My Button");
Icon imgicon = new ImageIcon("smiley_icon.gif");
JPanel panel = new JPanel();
button.setIcon(imgicon);
panel.add(button);
frame.add(panel, BorderLayout.NORTH);
frame.setSize(380, 380);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}