how to push the button using code, using the same effect as when the user presses

-1

I am doing the game for java and when starting the machine will give a random click drying on 4 buttons and then the user has to press the same as the machine, but I do not know how to make the machine push the button

package gênius;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.AWTException;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class tela extends JFrame {

    //nome do jogador e pontos
    int pontos = 0 ;
    String usuario = "";

    //varieaveis de jogadas
    String jjogador = "";
    String maquina ="";


    // janela de exibição
    public void janela(){
        JFrame  ftela = new JFrame();
        ftela.setTitle("Gênius");
        ftela.setSize(400, 600);
        ftela.setBackground(Color.white);
        ftela.setLocationRelativeTo(null);
        ftela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ftela.setVisible(true);


        //boões de ção
        JButton b1 = new JButton("");
        b1.setBounds(100, 220, 80, 80);
        b1.setVisible(true);
        b1.setBackground(Color.BLUE);


        JButton b2 = new JButton("");
        b2.setBounds(200, 220, 80, 80);
        b2.setVisible(true);
        b2.setBackground(Color.red);

        JButton b3 = new JButton("");
        b3.setBounds(100, 320, 80, 80);
        b3.setVisible(true);
        b3.setBackground(Color.GREEN);

        JButton b4 = new JButton("");
        b4.setBounds(200, 320, 80, 80);
        b4.setVisible(true);
        b4.setBackground(Color.YELLOW);

        JButton b5 = new JButton("vermalho");
        b5.setBounds(200, 420, 80, 80);
        b5.setVisible(true);
        b5.getColorModel();
        b5.setBackground(Color.WHITE);



        JPanel p1 = new JPanel();
        ftela.add(b1);
        ftela.add(b2);
        ftela.add(b3);
        ftela.add(b4);
        ftela.add(b5);


        //ações dos botôes
        b1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                play("b1");
                jjogador += " "+0;

            }
        });
        b2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                play("b2");
                jjogador += " "+1;

            }
        });
       b3.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                play("b3");
                jjogador += " "+2;

            }
        });
       b4.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                play("b4");
                jjogador += " "+3;

            }
        });

       b5.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, jjogador);

            }
        });





    }
     // coloando som no botão 
        public void play(String audio){
            URL url = getClass().getResource(audio+".wav");
            AudioClip  audioc = Applet.newAudioClip(url);
            audioc.play();
        }

        public void jlabe (){
            JLabel mPontos = new JLabel("pontos ");
            mPontos.setText("pontos :"+pontos);
            mPontos.setHorizontalAlignment(SwingConstants.CENTER);

        }

}
    
asked by anonymous 18.05.2016 / 06:17

2 answers

2

There are several ways to add action to the JButton, perhaps the simplest one is by anonymous class:

    b1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent evt) {
            //evento aqui
        }
    });

This form is often used when nothing is too complex in the action. If you're using 8, things get a lot easier:

b1.addActionListener(e -> {
            //ação aqui
        });
    
18.05.2016 / 12:27
0

The easiest way is this: You add the action to Button:

b1.addActionListener(this);

Then you'll create this action:

public void actionPerformed(ActionEvent e) {
        //O código que queiras
}

But for this to be possible, you have to implement a ActionListener :

public class NOME_DA_CLASSE implements ActionListener{
    //Código    
}
    
18.05.2016 / 08:59