This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GUI Buttons Help

 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic