import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*in main*/
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println('x');
}
});
import java.awt.event.ActionListener; //For action listener
import javax.swing.*; //For JButton
// Inside of function or method
JButton button = new JButton();
button.addActionListener(e -> {
System.out.println("Another way of writing an action listener.");
})
import javax.swing.*;
import java.awt.event.*;
public class JButtonExample
{
public static void main(String[] args)
{
JFrame frame = new JFrame("ActionListener Example");
JButton btn = new JButton("Click here");
btn.setBounds(70,80,100,30);
//Change button text on click
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
btn.setText("OK");
}
});
frame.add(btn);
frame.setSize(250,250);
frame.setLayout(null);
frame.setVisible(true);
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button) {
button.setText("Button Clicked!");
}}
JButton Button = new JButton();
Button.setSize(300,300);
Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
System.out.println(BlockNumber)
}
});
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("hello world");
}
});
A semantic event which indicates that a component-defined action
occurred. This high-level event is generated by a component
(such as a Button) when the component-specific action occurs
(such as being pressed). The event is passed to every ActionListener
object that registered to receive such events using the component's
addActionListener method.