简体   繁体   中英

Java awt calculator +/- , how to replace + with - : 1st click = “-”, 2nd click = “+” and so on

[SOLVED]

I am working for Calculator application and I am stuck a little bit. For example I need to work to +/- button. How can I replace - with + for example? Just like windows Calculator does: First click = -, Second click = + and so on. I have some code, I used MouseListener to count clicks. I will post my if here. I work with awt, not swing yet.

 if(click %2 !=0)
 {
    text.setText("-"+text.getText());
 }
 else
 {
    text.setText(""+text.getText());
 }

text is object of TextField . If you need more details I will add parts of my code here. Thank you

Try this one:

if(click %2 !=0){
    text.setText("-"+text.getText());
}
else{
    // remove your first character (- sign)
    text.setText(text.getText().substring(1));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM