import java.awt.*; import java.awt.event.*; import javax.swing.*; public class jbutton { static JFrame frame = new JFrame("Example"); public static void main(String[] args) { ImageIcon icon = new ImageIcon("spam.jpg"); JButton jb = new JButton("
press
for
spam", icon); // The code to handle events from button: jb.addActionListener( new ActionListener() { int i = 1; public void actionPerformed(ActionEvent e) { System.out.println("pressed "+ i++); } } ); frame.setSize(500,300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout( new FlowLayout() ); frame.add( jb ); frame.pack(); // size the JFrame to fit its contents frame.setVisible(true); } }