Below code shows how to setActionCommand and addActionListener to a JButton /** * * @author Eric * www.techoverload.net */ import ja...
/** * * @author Eric * www.techoverload.net */ import javax.swing.JFrame; import javax.swing.JButton; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ButtonActionCommand extends JFrame implements ActionListener{ ButtonActionCommand() { JButton button=new JButton(new MyIcon()); button.setBounds(10, 10, 100,50);//set button location //setActionCommand for the button button.setActionCommand("Button clicked"); //add action listener to the button button.addActionListener(this); add(button);//add button setLayout(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400,400); setVisible(true); } public void actionPerformed(ActionEvent evt) { System.out.println(evt); } public static void main(String args[]) { new ButtonActionCommand(); } }