I have this code, which generates a window with a button.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PruebasGraficas {
public static void main(String[] args) {
JFrame Ventana1 = new JFrame();
Ventana1.setTitle("VENTANA 1");
Ventana1.setSize(300, 300);
Ventana1.setLocation(500, 300);
Ventana1.setVisible(true);
JButton boton1 = new JButton();
boton1.setText("EXECUTE");
boton1.setVisible(true);
Ventana1.getContentPane().setLayout(new FlowLayout());
Ventana1.add(boton1);
}
}
I would like to know what would be the most correct way to add an action to the button dynamically.
In this case, when you click on the button, it executes an action.