JButton with Icon

The swing icons are encapsulated by ImageIcon class. This class paints an image from the image.

ImageIcon(String filename)

Program

hjghjghjh

 

 

 

Program Source

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.FlowLayout;

class Frame extends JFrame {
 
    Frame()
    {
        setTitle("JButton with Icon");
        setLayout(new FlowLayout());
        setButtons();
        setSize(700, 200);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
 
    private void setButtons()
    {
        JButton jb1 = new JButton("JButton01",new ImageIcon("icon1.png"));
        JButton jb2 = new JButton("JButton02",new ImageIcon("icon2.png"));
        JButton jb3 = new JButton("JButton03",new ImageIcon("icon3.png"));
        add(jb1);
        add(jb2);
        add(jb3);
    }
}

public class Javaapp {
 
    public static void main(String[] args) {
     
        Frame frame = new Frame();
    }
}

Leave a Comment