Two Laptop Bag
The moose likes Beginning Java and the fly likes GUI Buttons Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "GUI Buttons Help" Watch "GUI Buttons Help" New topic

GUI Buttons Help

Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 249

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

Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

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?
Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 249

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
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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

[Jess in Action][AskingGoodQuestions]
Greg Brannon

Joined: Oct 24, 2010
Posts: 563
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.

Good luck.

Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 249

@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

Thanks all!

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46326
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.
I agree. Here's the link:
subject: GUI Buttons Help
It's not a secret anymore!