Java ActionListener actionPerformed (ActionEvent e)

Syntax

ActionListener.actionPerformed(ActionEvent e) has the following syntax.

void actionPerformed(ActionEvent e)

Example

In the following code shows how to use ActionListener.actionPerformed(ActionEvent e) method.


//from www .j a  v a 2  s .  co m

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

class ButtonListener implements ActionListener {
  ButtonListener() {
  }

  public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("Button1")) {
      System.out.println("Button1 has been clicked");
    }
  }
}
public class Main extends JPanel {

  public Main() {

    JButton btn1 = new JButton("Button1");
    btn1.addActionListener(new ButtonListener());
    add(btn1);

  }

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new Main());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setVisible(true);
  }
}




















Home »
  Java Tutorial »
    java.awt.event »




AdjustmentListener
ActionEvent
ActionListener
HierarchyEvent
HierarchyListener
InputEvent
ItemEvent
ItemListener
KeyAdapter
KeyEvent
KeyListener
MouseAdapter
MouseEvent
MouseListener
MouseMotionAdapter
MouseMotionListener
MouseWheelEvent
MouseWheelListener
WindowAdapter
WindowEvent
WindowFocusListener
WindowStateListener