Jbutton shoot two different events?

Asked

Viewed 507 times

2

I have a program that simulates a game of dice, where I have two "dice" that, when the sum of the two gives 7, the user is winner, if not, he loses.

Well, I made a simple interface on swing, where I created two buttons, a launches the first value using the class Random, and another button that throws a second value, and one more that brings the result.

I wonder if it is possible, and if it is, how to make the user click on only one button, to trigger the two values, he clicks once, triggers the value of the first dice, and he clicks on another, and triggers the value of the second dice. Thank you!

Follows Code from the Screen:

public class JDialogJogar extends javax.swing.JDialog {

    /**
     * Creates new form Jogo
     */
    public JDialogJogar(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    int numero1;
    int numero2;

    public void jogarDado1() {

        try {

            Random gerador1 = new Random();
            numero1 = gerador1.nextInt(7) + 1;          


            JOptionPane.showMessageDialog(this, "Dado 1: " + numero1);
            int dado1 = numero1;
            txtDado1.setText(String.valueOf(numero1));
            travarDado1();


        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Erro" + e.getMessage());
        }

    }

    public void jogarDado2() {

        try {

            Random gerador = new Random();
            numero2 = gerador.nextInt(7 + 1);

            JOptionPane.showMessageDialog(this, "Dado 2: " + numero2);
            int dado2 = numero2;
            txtDado2.setText(String.valueOf(numero2));
            travarDado2();

        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Erro" + e.getMessage());
        }
    }

    public void mostrarResultado() {

        int resultado = numero1 + numero2;

        if ((numero1 + numero2) == 7) {
            JOptionPane.showMessageDialog(this, "Resultado: " + resultado + " Parabéns! Você Ganhou!");
            destravar();
            limpar();

        } else {

            JOptionPane.showMessageDialog(this, "Resultado: " + resultado + " Você Perdeu!");
            destravar();
            limpar();

        }

    }

Of the action of the button:

private void btnResultadoActionPerformed(java.awt.event.ActionEvent evt) {                                             
        mostrarResultado();
    }                                            

    private void btnJogarDado1ActionPerformed(java.awt.event.ActionEvent evt) {                                              
        jogarDado1();

    }                                             

    private void btnJogarDado2ActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
        jogarDado2();
    }       
  • You want with one click, fire dice 1 and 2 or with different clicks?

  • I want you to click, fire dice 1, and with another click, fire dice 2.

  • The data can continue to be fired freely after they have already been fired once each?

  • Can Yes!------

1 answer

1

Create a boolean variable in your class JDialogJogar to control when the button has already been clicked previously:

boolean hasBeenClicked = false;

And in the actionPerformed() of the button that will trigger the data, check if the button has already been clicked previously, changing the variable above, as if it were a "switch":

private void btnJogarDadosActionPerformed(java.awt.event.ActionEvent evt) {                                              
    if(!hasBeenClicked) {
       jogarDado1();
       hasBeenClicked = true;

    } else {
       jogarDado2();
       hasBeenClicked = false;  
}

The variable hasBeenClicked will control when the button was clicked earlier, indicating which of the two methods should be triggered, and after the second method is executed, we set as false to "clear" the information for another 2-move cycle.

  • 1

    Thank you so much! It worked perfectly! Hugs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.