Share this page 

Have an JButton with an ImageTag(s): Swing


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

 public class TestJButton extends JApplet
 {
   JButton b1, b2, b3;

   public void init() {
     setLayout(new FlowLayout());
     try {
         b1 = new JButton("Regular",
            new ImageIcon
                (new URL("http://www.rgagnon.com/images/gumby.gif")));
         add(b1);
         // text on the center
         b2 = new JButton("Special",
            new ImageIcon
                (new URL("http://www.rgagnon.com/images/gumby.gif")));
         b2.setHorizontalTextPosition(SwingConstants.CENTER);
         add(b2);
         // text on the top
         b3 = new JButton("Special",
            new ImageIcon
                (new URL("http://www.rgagnon.com/images/gumby.gif")));
         b3.setVerticalTextPosition(SwingConstants.TOP);
         b3.setHorizontalTextPosition(SwingConstants.CENTER);
         add(b3);

     }
     catch (Exception e) {
         e.printStackTrace();
     }
   }
}
How it looks :