Add Icon to JButton in Java
Description
The following code shows how to add Icon to JButton.
Example
//from w ww .j av a2 s .c om
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame("DefaultButton");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Icon warnIcon = new ImageIcon("yourFile.gif");
JButton button2 = new JButton(warnIcon);
frame.add(button2);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »