Home > Back-end >  What is is the difference between these two codes?
What is is the difference between these two codes?

Time:01-07

This code right here is a button of a calculator that is when pressed shows "2" in the calculator's textbox I was just curious why the first code allows me to enter number "2" just once

while the second one allows me to enter the number "2" Many times, in other words, what does textbox.getText() adds to this code?

private void BUT2ActionPerformed(java.awt.event.ActionEvent evt) {                                     
    String enternumber =  BUT2.getText();
    textbox.setText(enternumber);
} 
private void BUT2ActionPerformed(java.awt.event.ActionEvent evt) {                                     
    String enternumber = textbox.getText()  BUT2.getText();
    textbox.setText(enternumber);
} 

CodePudding user response:

The first snippet copies the label of the button ("2") into the text field, replacing its prior content.

The second snippet looks at what's already in the text field, which could be anything, and adds the label of the button ("2") to the end of it. This way, you can add "2" to the text field as many times as you press the button.

  •  Tags:  
  • Related