parameter ejemplo botones java swing jframe actionlistener

java - ejemplo - ActionListener para botones



botones en jsf (3)

Es porque la construcción / adición de su actionlistener está anidada en otro actionlistener, al. Por lo tanto, los oyentes de acción que le interesan (los que muestran los mensajes) en realidad no se están agregando a los botones hasta que haga clic una vez. Agregar los detectores de acción a sus botones en launchJFrame () soluciona el problema. Aquí hay una versión fija de tu código:

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MessageBoxes{ private JButton alert = new JButton("Alert"); private JButton yesNo = new JButton("Yes/No"); private JButton color = new JButton("Color"); private JButton vals = new JButton("3 Vals"); private JButton input = new JButton("Input"); private JTextField txt = new JTextField(15); private JFrame frame; public MessageBoxes(){ frame = new JFrame("Title"); frame.setSize(250, 250); frame.getContentPane().setLayout(new FlowLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(alert); frame.getContentPane().add(yesNo); frame.getContentPane().add(color); frame.getContentPane().add(vals); frame.getContentPane().add(input); frame.getContentPane().add(txt); frame.setVisible(true); } public void launchJFrame(){ alert.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //Execute when button is pressed JOptionPane.showMessageDialog(null, "There''s a bug on you!", "Hey!", JOptionPane.ERROR_MESSAGE); } }); yesNo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //Execute when button is pressed int val = JOptionPane.showConfirmDialog(null, "Choose yes or no", "Your Call...", JOptionPane.YES_NO_OPTION); if(val != JOptionPane.CLOSED_OPTION){ if(val == 0){ txt.setText("Yes"); } else{ txt.setText("No"); } } } }); color.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //Execute when button is pressed Object[] options = {"Red", "Green"}; int sel = JOptionPane.showOptionDialog(null, "Choose a Color!", "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]); if(sel != JOptionPane.CLOSED_OPTION){ txt.setText("Color Selected: " + options[sel]); } } }); vals.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //Execute when button is pressed Object[] selections = {"First", "Second", "Third"}; Object val = JOptionPane.showInputDialog(null, "Choose one", "Input", JOptionPane.INFORMATION_MESSAGE, null, selections, selections[0]); if(val != null){ txt.setText(val.toString()); } } }); input.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //Execute when button is pressed String val = JOptionPane.showInputDialog("How mant fingers do you see?"); txt.setText(val); } }); } public static void main(String [] args){ MessageBoxes messageBoxes = new MessageBoxes(); messageBoxes.launchJFrame(); } }

Así que he desarrollado un programa para una clase mía para usar ActionListener para eventos cuando se hace clic en un botón. Tengo el programa funcionando, sin embargo, al hacer clic en cualquier botón, tiene que hacer clic varias veces en los botones de entrada para que la respuesta se registre y pase al siguiente nivel del programa.

Ejemplo 1: Botón de alerta abierto 2 a veces 3 "¡Hola, hay un error en ti!" diálogos de mensaje.

example2: Sí / No le pide 3-4 veces para devolver su respuesta de sí / no a txt.

example3: Color toma 3/4 clics antes de devolver la entrada a txt.

(Creo que te lo puedes imaginar...)

Por mi vida no puedo descubrir por qué no solo tomará una entrada y continuará ...

El código de mi programa para su revisión ... Gracias de antemano.

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MessageBoxes{ private JButton alert = new JButton("Alert"); private JButton yesNo = new JButton("Yes/No"); private JButton color = new JButton("Color"); private JButton vals = new JButton("3 Vals"); private JButton input = new JButton("Input"); private JTextField txt = new JTextField(15); private JFrame frame; private ActionListener al = new ActionListener(){ public void actionPerformed(ActionEvent e){ alert.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //Execute when button is pressed JOptionPane.showMessageDialog(null, "There''s a bug on you!", "Hey!", JOptionPane.ERROR_MESSAGE); } }); yesNo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //Execute when button is pressed int val = JOptionPane.showConfirmDialog(null, "Choose yes or no", "Your Call...", JOptionPane.YES_NO_OPTION); if(val != JOptionPane.CLOSED_OPTION){ if(val == 0){ txt.setText("Yes"); } else{ txt.setText("No"); } } } }); color.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //Execute when button is pressed Object[] options = {"Red", "Green"}; int sel = JOptionPane.showOptionDialog(null, "Choose a Color!", "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]); if(sel != JOptionPane.CLOSED_OPTION){ txt.setText("Color Selected: " + options[sel]); } } }); vals.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //Execute when button is pressed Object[] selections = {"First", "Second", "Third"}; Object val = JOptionPane.showInputDialog(null, "Choose one", "Input", JOptionPane.INFORMATION_MESSAGE, null, selections, selections[0]); if(val != null){ txt.setText(val.toString()); } } }); input.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //Execute when button is pressed String val = JOptionPane.showInputDialog("How mant fingers do you see?"); txt.setText(val); } }); } }; public MessageBoxes(){ frame = new JFrame("Title"); frame.setSize(250, 250); frame.getContentPane().setLayout(new FlowLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(alert); frame.getContentPane().add(yesNo); frame.getContentPane().add(color); frame.getContentPane().add(vals); frame.getContentPane().add(input); frame.getContentPane().add(txt); frame.setVisible(true); } public void launchJFrame(){ alert.addActionListener(al); yesNo.addActionListener(al); color.addActionListener(al); vals.addActionListener(al); input.addActionListener(al); } public static void main(String [] args){ MessageBoxes messageBoxes = new MessageBoxes(); messageBoxes.launchJFrame(); } }


Aquí está la solución que encontré que funciona mejor para mí:

private ActionListener alertAction = new ActionListener(){ public void actionPerformed(ActionEvent e){ //Execute when button is pressed JOptionPane.showMessageDialog(null, "There''s a bug on you!", "Hey!", JOptionPane.ERROR_MESSAGE); } }; private ActionListener yesNoAction = new ActionListener(){ public void actionPerformed(ActionEvent e){ //Execute when button is pressed //Execute when button is pressed int val = JOptionPane.showConfirmDialog(null, "Choose yes or no", "Your Call...", JOptionPane.YES_NO_OPTION); if(val != JOptionPane.CLOSED_OPTION){ if(val == 0){ txt.setText("Yes"); } else{ txt.setText("No"); } } } }; private ActionListener colorAction = new ActionListener(){ public void actionPerformed(ActionEvent e){ //Execute when button is pressed Object[] options = {"Red", "Green"}; int sel = JOptionPane.showOptionDialog(null, "Choose a Color!", "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]); if(sel != JOptionPane.CLOSED_OPTION){ txt.setText("Color Selected: " + options[sel]); } } }; private ActionListener valsAction = new ActionListener(){ public void actionPerformed(ActionEvent e){ //Execute when button is pressed Object[] selections = {"First", "Second", "Third"}; Object val = JOptionPane.showInputDialog(null, "Choose one", "Input", JOptionPane.INFORMATION_MESSAGE, null, selections, selections[0]); if(val != null){ txt.setText(val.toString()); } } }; private ActionListener inputAction = new ActionListener(){ public void actionPerformed(ActionEvent e){ //Execute when button is pressed String val = JOptionPane.showInputDialog("How mant fingers do you see?"); txt.setText(val); } }; public MessageBoxes(){ frame = new JFrame("Title"); frame.setSize(250, 250); frame.getContentPane().setLayout(new FlowLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(alert); frame.getContentPane().add(yesNo); frame.getContentPane().add(color); frame.getContentPane().add(vals); frame.getContentPane().add(input); frame.getContentPane().add(txt); frame.setVisible(true); } public void launchJFrame(){ alert.addActionListener(alertAction); yesNo.addActionListener(yesNoAction); color.addActionListener(colorAction); vals.addActionListener(valsAction); input.addActionListener(inputAction); }


El problema es que cada vez que se llama actionPerformed dentro de su ActionListener al , está registrando nuevos ActionListener con sus botones

private ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent e) { alert.addActionListener(new ActionListener() { //... }); yesNo.addActionListener(new ActionListener() { //... }); color.addActionListener(new ActionListener() { //... }); vals.addActionListener(new ActionListener() { //... }); input.addActionListener(new ActionListener() { //... }); } };

Entonces, cada vez que se actionPerformed método actionPerformed , cada botón registra un NEW actionListener .

En cambio, podría usar una instrucción if-else para determinar el origen del evento, por ejemplo ...

Object source = e.getSource(); if (source == alert) { JOptionPane.showMessageDialog(null, "There''s a bug on you!", "Hey!", JOptionPane.ERROR_MESSAGE); } else if (...

O puede deshacerse del ActionListener al completo y registrar simplemente los ActionListener individuales en los botones dentro del método launchJFrame ...

public void launchJFrame() { alert.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //Execute when button is pressed } }); yesNo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //Execute when button is pressed int val = JOptionPane.showConfirmDialog(null, "Choose yes or no", "Your Call...", JOptionPane.YES_NO_OPTION); if (val != JOptionPane.CLOSED_OPTION) { if (val == 0) { txt.setText("Yes"); } else { txt.setText("No"); } } } }); color.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //Execute when button is pressed Object[] options = {"Red", "Green"}; int sel = JOptionPane.showOptionDialog(null, "Choose a Color!", "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]); if (sel != JOptionPane.CLOSED_OPTION) { txt.setText("Color Selected: " + options[sel]); } } }); vals.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //Execute when button is pressed Object[] selections = {"First", "Second", "Third"}; Object val = JOptionPane.showInputDialog(null, "Choose one", "Input", JOptionPane.INFORMATION_MESSAGE, null, selections, selections[0]); if (val != null) { txt.setText(val.toString()); } } }); input.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //Execute when button is pressed String val = JOptionPane.showInputDialog("How mant fingers do you see?"); txt.setText(val); } }); }