How do I change the increment of a button by another button?

Asked

Viewed 451 times

2

I have a button that adds a variable: x + 1, and every time I click on that button, the variable will increase 1 unit.

But how do I program a button to change the function of button 1, so instead of x + 1, be it x + 2

The buttons, and all that, are being programmed into Java JFrame with the program netBeans.

private int xi;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         



    xi++;
    jLabel2.setText("Your money  " + xi + "$");

}                                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    //E neste butao que quero fazer a funcao.


}
  • Add how is your code attempt so far in question.

  • Look young, it depends. If it’s just changing the increased amount of 1 for 2, I would use a variable that defines this quantity and change the value of it. Now, if you want to change the whole function, there’s another story.

  • @jbueno is not very complicated, in fact, I already have a better suggestion here in mind, but I need to see his code first, so then not have to adapt all the time.

  • Yes, I know it’s simple.

  • Here’s a sample of the code

  • The variation is only +1 to +2? If that’s it, you can do it using togglebutton

  • Okay, I’ll explain a little more what I want to do. I want to play a little game where by clicking on the button1 Victory will win another $1. Ai, Voce can buy an "upgrade" where it will cost $150 so that when you click on button2, Voce earns +2 per click. So I can not use togglebutton because Voce can only buy 1 time.

  • The situation you are explaining can be done with 2 different buttons, but in question you want to do with the same.

  • Sm! Basically, I have the boot 1 doing a function. Then, I want, by clicking on the button 2, I want it to change function, so that instead of adding up 1, add up 2.

  • If you want to add 2-in-2, just use xi += 2; instead of xi++ in the second boot. That’s it?

  • That’s not really what I want to do. I want that when clicking button 2, butao1 will start adding 2 in the variable xi. The button 2 is only used to change the function of the button 1 and is no longer used.

  • The idea is the same as I said, only with an extra variable to control the increment, rather than with the operator ++. Button 2 can only be triggered once?

  • Yes, Boot 2 is only triggered 1 time

Show 8 more comments

2 answers

4


One of the ways to do this would be to use a unique variable to represent the "increment":

private int xi = 1;
private int increment = 1;

Then, on the first button, you change to the following:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

    xi += increment;
    jLabel2.setText("Your money  " + xi + "$");

} 

And on the second button:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { 

    increment = 2;//ou increment++;
    //desativa o botao apos o primeiro uso
    ((JButton) evt.getSource()).setEnable(false); 

}

The way it is, you don’t even need to disable the button, as much as you click on it, the change will always be to 2. If you use increment++, you need to use the code that disables the button to stop the increment.

  • was commenting on this, was bad!

  • @Thomasbrazpinto did not understand.

  • Unfortunately it is giving me some kind of error in "xi+increment;". The error hint says "not a statement".

  • @Guilhermefigueira corrected, little increment error on button1.

  • 1

    Thanks a lot @diegofm finally worked out!

  • is that I commented exactly the same thing as you (or almost) but while I was typing, you answered and I didn’t see, so I said

Show 1 more comment

2

Dude, you can use a variable to do this! It’s pretty simple:

private int xi;
private int xi2=1;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    xi+=xi2;
    jLabel2.setText("Your money  " + xi + "$");
}                                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    xi2+=1;//aqui você muda o tanto que vale, é só adicionar mais um, no caso depende do que você quer adicionar,
    //se você mudar o valor aqui, pode colocar + 150 até, que aí a cada clique vai adicionar + 150
}

Browser other questions tagged

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