Está en la página 1de 1

import javax.swing.*; import java.awt.*; import java.awt.event.

*; public class Genera_botones_by_LuisF extends JFrame implements ActionListener { JButton [] boton; int cantidad_botones; public Genera_botones_by_LuisF() { cantidad_botones=(int)(Math.random()*19+1); boton = new JButton[cantidad_botones]; Construye_botones(cantidad_botones); this.setLayout(new FlowLayout()); this.setSize(250,400); this.setVisible(true); this.setDefaultCloseOperation(3); } public void Construye_botones(int cantidad_botones) { for(int i=0;i<cantidad_botones;i++) { boton[i]= new JButton("Botn "+(i+1)); boton[i].addActionListener( this ); this.add(boton[i]); } } public void actionPerformed(ActionEvent e) { Object fuente=e.getSource(); for(int j=0;j<cantidad_botones;j++) { if(fuente.equals(boton[j])) { JOptionPane.showMessageDialog(null,"Usted ha Presionado el Botn N "+(j+1)); } } } public static void main(String[] args) { Genera_botones_by_LuisF g = new Genera_botones_by_LuisF(); } }

También podría gustarte