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.
– user28595
Look young, it depends. If it’s just changing the increased amount of
1
for2
, 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.– Jéf Bueno
@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.
– user28595
Yes, I know it’s simple.
– Jéf Bueno
Here’s a sample of the code
– Guilherme Figueira
The variation is only +1 to +2? If that’s it, you can do it using togglebutton
– user28595
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.
– Guilherme Figueira
The situation you are explaining can be done with 2 different buttons, but in question you want to do with the same.
– user28595
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.
– Guilherme Figueira
If you want to add 2-in-2, just use
xi += 2;
instead ofxi++
in the second boot. That’s it?– user28595
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.
– Guilherme Figueira
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?– user28595
Yes, Boot 2 is only triggered 1 time
– Guilherme Figueira