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

Asked

Viewed 878 times

-1

I’m making the game for java and when starting the machine will give a random sequence of clicks on 4 buttons and then the user has to press the same machine, but I do not know how to make the machine press 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);

        }

}
  • I think I get it, but your approach is wrong, I’m going to try to make an example here in a way that can meet.

  • please, I’m in a bit of a bind because I don’t even know what to use to do this

  • you know how to ?

  • Is there any way you can add what you already asked in the question? Showing from scratch will end up generating a more complex response than necessary for the question, it is easier to try high from its starting point.

  • have yes I’ll add

2 answers

2

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

    b1.addActionListener(new ActionListener() {

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

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

b1.addActionListener(e -> {
            //ação aqui
        });
  • no, that I did but I want to press this button using code I’ve seen robot but robot has to move the mouse wanted something more direct

  • @Diogocipriano did not understand, on the question, you question how to add an action in Jbutton only.

  • the person goes on the screen and presses the correct button, I wanted to press the button using code

  • @Diogocipriano then believe that this is not the correct approach to follow. What or which component will trigger the action of the boot? Edit the question and provide more details of what you’re trying to do and what you need to do.

  • I want to use a for, and when player click the start button I will use a Random so that when the Random generate 1 the code click the 1 button, and not the user

  • @Diogocyprian, why use a button for this? Perhaps it is better to use a method that receives this generated value, you treat this value and execute the action according to the past value. You can do this using switch...case, but you would have to do what I told you, edit the question and provide better details about what you’re doing.

Show 1 more comment

0

The easy way is like this: Add the action to Button:

b1.addActionListener(this);

Then you’ll create that action:

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

However, in order for this way to be possible, you must implement a ActionListener:

public class NOME_DA_CLASSE implements ActionListener{
    //Código    
}
  • 2

    What the this represents? Because if the class is not an actionListener, your code will fail.

  • Yes Diego F, we have to put this. public class NOME_DA_CLASSE Implements Actionlistener

  • Thank you, I’ll update you right away.

  • 1

    Thanks Diego F, I’ll update you right away

Browser other questions tagged

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