Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

201
Vistas
¿Cómo forzar que se vuelva a llamar al bucle for?

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++; } } }
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda