2
I’m starting in GUI in Java and wanted to understand how it works to capture events on a button, as for example in the code below:
Simplegui1.java
import javax.swing.*;
import java.awt.event.*;
public class SimpleGui1 implements ActionListener {
JButton button;
public static void main( String[] args ) {
SimpleGui1 gui = new SimpleGui1();
gui.go();
} // fim do método main
public void go() {
JFrame frame = new JFrame();
button = new JButton( "click me" );
button.addActionListener( this );
frame.getContentPane().add( button );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize( 300, 300 );
frame.setVisible( true );
} // fim do método go
public void actionPerformed(ActionEvent event) { // Aqui está minha dúvida
// Em que momento é chamado o
button.setText( "I've been clicked!" ); // método?
} // fim do método actionPerformed
} // fim da classe SimpleGui1
How does this communication between the origin of the event (button) and the listener of the event?
Interface ActionListener
in the documentation:
public abstract interface java.awt.event.ActionListener extends java.util.EventListener {
// Method descriptor #8 (Ljava/awt/event/ActionEvent;)V
public abstract void actionPerformed(java.awt.event.ActionEvent arg0);
}
Thank you!