Java Programming/Applets/ActionListener
From Wikibooks, the open-content textbooks collection
ِActionListener is an interface that could be implemented in order to determine how certain event should be handled. When implementing an interface, all methods in that interface should be implemented, ActionListener interface has one method to implement named actionPerformed.
The following example shows how to implement ActionListener:
importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;classActionDemoextendsJFrame implements ActionListener{publicActionDemo() { setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(300, 300); getContentPane().setLayout(new FlowLayout()); JButton b =newJButton("Click me"); getContentPane().add(b); b.addActionListener(this); }publicvoidactionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(this, "Hi !!!"); }publicstaticvoidmain(String[] args) {newActionDemo().setVisible(true); } }
When you compile and run the above code, a message will appear when you click on the button.

