Java JButton(String text, Icon icon) Constructor

Syntax

JButton(String text, Icon icon) constructor from JButton has the following syntax.

public JButton(String text,   Icon icon)

Example

In the following code shows how to use JButton.JButton(String text, Icon icon) constructor.


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("Warn.gif");
    JButton button3 = new JButton("Warning", warnIcon);
    frame.add(button3);//w ww.  j  a v  a 2s.  c o  m
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}