JButton trigger two different events?

2

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

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

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

Follow Screen Code:

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();

        }

    }

Button action:

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();
    }       
    
asked by anonymous 23.05.2016 / 03:37

1 answer

1

Create a Boolean variable in your JDialogJogar class to control when the button was previously clicked:

boolean hasBeenClicked = false;

And in the% of the button that will trigger the data, check if the button was previously clicked, changing the above variable as a "switch":

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

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

The variable actionPerformed() will control when the button was previously clicked, indicating which of the two methods should be triggered, and after the second method is executed, let's select hasBeenClicked to "clear" the information for another cycle of 2 plays.

    
23.05.2016 / 05:08