java button actionListener

w‮ttual.ww‬uri.com
java button actionListener

To add an action to a java button, you can use the 'ActionListener' interface of the 'java.awt.event' library. The 'ActionListener' interface has a single method, 'actionPerformed', which is called when the button is clicked.

Here's an example of how to add an action to a button using the 'ActionListener' interface:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;

public class Main {
  public static void main(String[] args) {
    // Creates a button
    JButton botao = new JButton("Clique aqui");

    // Adds the action to the button
    botao.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        // Action code here...
      }
    });
  }
}

When the button is clicked, the 'actionPerformed' method is called and you can place the desired action code inside it.

Note that in this example, we are using the 'JButton' class of the 'javax.swing' library, which is a Java graphical user interface (GUI) class. If you are using another graphical interface library or a web application framework, you may need to use a different button class or a different method to add an action to the button. See the documentation for the library or framework you are using for more information.

Created Time:2017-11-03 00:22:23  Author:lautturi