The below GUI code is a simple scorekeeping program. The problem is the buttons. They're not working like they should. You'll understand when you run it. I used the NetBeans GUI maker for this; I'm not an experienced programmer. I've only had one year of Java in school. Anyways, it would be nice if you could change it / tell me what to change. Thanks
Okay. I really didn't want to download your code and look for bugs which were too complicated to be described in a forum post. So I just browsed the code looking for things which might be wrong. I did find this:
It seems to me that something which is supposed to be adding 10 to Team 2's score shouldn't be starting from "team1Score".
However if that isn't what you had in mind, then I would recommend you spend a couple of minutes to figure out what your problem is and to post an explanation of the problem. What happens, when does it happen, and what should happen instead?
Paul Clapham wrote:
What happens, when does it happen, and what should happen instead?
Sorry, I should've been more detailed. And thanks for pointing out that error. (The making of sloppy copy-pasting)
When I press a button, Ex: "+10", it will change the score to 10. That's fine and dandy, but when I click the button again, the score doesn't change. If I would then click "+5", the score would just change to 5. So basically, there is no addition or subtraction happening. Or if there is, it is just adding or subtracting from 0 and not the previous number.
What should happen instead? Clicking "+5", then "+10", then "+10", then "-5", and then "-5" should result in the displaying of the number 15 instead of -5 (the last number pressed).
I hope that cleared things up.
-Joel Amos Christophel
The problem is that you set the label's text to "team1Score + 10", but you never set "team1Score" to anything other than 0, so it remains 0, and scores don't accumulate. You'll want to do something like
Swing is pretty easy to learn and code "by hand." The Netbeans GUI Builder creates complicated code that is nearly impossible for a human to read, making the resulting code difficult or impossible to modify, reuse, and maintain. I recommend you learn to code Swing yourself. There are excellent tutorials available for free, and there is always help available.
Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
@Ernest Friedman-Hill Yup, that would be precisely the problem. Thanks for helping. =)
@Greg Brannon I would learn it, except that 1) I don't feel like my knowledge of the basics if firm enough yet and 2) I only wish to make one GUI program for now and for a specific purpose, so it seems kind of useless to learn lots more things for one specific occasion
I agree with Greg Brannon. The approach of "I only need it this once" will result in your never learning to do it right. You should of course start with the basics, and learn slowly, starting with simple things.