ActionListener Event Source In Java ActionPerformed Method
Chapter:
Action Listener
Last Updated:
28-07-2016 18:30:09 UTC
Program:
/* ............... START ............... */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JavaEventSourceExample extends JFrame implements ActionListener {
private JButton button1 = new JButton("Click Me!");
private int clickCount = 0;
public static void main(String[] args) {
new JavaEventSourceExample();
}
public JavaEventSourceExample() {
this.setSize(200, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("I'm Listening");
JPanel panel1 = new JPanel();
button1.addActionListener(this);
panel1.add(button1);
this.add(panel1);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
clickCount++;
if (clickCount == 1)
button1.setText("I've been clicked!");
else
button1.setText("I've been clicked" + clickCount + " times!");
}
}
}
/* ............... END ............... */
Output
Tags
ActionListener Event Source, Java, ActionListener