Hola, tengo este código simple que genera botones y sus acciones en un bucle. Bucle predeterminado como 1 paso, lo que significa 1 botón.
Mi objetivo es programar el botón "Adicionar" para incrementar el bucle y hacer que el bucle genere más botones.
aquí está el código:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @SuppressWarnings("serial") public class GuiClass extends JFrame{ // PUBLIC VARS // VARIAVEIS public int numeroButoes = 1; // CONSTRUCTOR public GuiClass(){ super("Loop Buttons"); setLayout(null); // BUTOES ADD & REMOVE JButton mais = new JButton("Adiciona"); //JButton menos = new JButton("Remove"); mais.addActionListener(new handlerBotaoAdd()); mais.setBounds(50, 0, 100, 40); add(mais); // LOOP BOTOES for (int x = 0; x < numeroButoes; x++ ){ JButton teste = new JButton("Botão " + x); teste.setActionCommand("Botão " +x); teste.addActionListener(new handlerBotoesLoop(teste.getActionCommand())); teste.setBounds(450, (x == 0 ? 0: x+(40*x)), 100, 40); add(teste); } } private class handlerBotoesLoop implements ActionListener{ String texto; public handlerBotoesLoop(String x){ x = texto; } public void actionPerformed(ActionEvent event){ JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand())); } } private class handlerBotaoAdd implements ActionListener{ public void actionPerformed(ActionEvent event){ numeroButoes++; } } }
Puede usar un Método para poner la lógica para crear botones y llamarlo desde el evento de clic del botón
import javax.swing.*; import java.awt.event.*; @SuppressWarnings("serial") public class Abc extends JFrame{ // PUBLIC VARS // VARIAVEIS public int numeroButoes = 1; // CONSTRUCTOR public Abc(){ super("Loop Buttons"); setLayout(null); // BUTOES ADD & REMOVE JButton mais = new JButton("Adiciona"); mais.addActionListener(new handlerBotaoAdd()); //JButton menos = new JButton("Remove"); mais.addActionListener(new handlerBotaoAdd()); mais.setBounds(50, 0, 100, 40); add(mais); } // Method for button creating private void generateButtons(){ for (int x = 0; x < numeroButoes; x++ ){ JButton teste = new JButton("Botão " + x); teste.setActionCommand("Botão " +x); teste.addActionListener(new handlerBotoesLoop(teste.getActionCommand())); teste.setBounds(450, (x == 0 ? 0: x+(40*x)), 100, 40); add(teste); } } private class handlerBotoesLoop implements ActionListener{ String texto; public handlerBotoesLoop(String x){ x = texto; } @Override public void actionPerformed(ActionEvent event){ JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand())); } } private class handlerBotaoAdd implements ActionListener{ @Override public void actionPerformed(ActionEvent event){ numeroButoes++; generateButtons(); } } }
No puedo entender si desea crear un solo botón o más de un botón cuando hace clic en el botón Agregar. En mi solución, cada vez que haga clic en el botón Agregar se creará más de un botón.