• Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing Caffeine Calculator v1  RSS feed

 
Adam Turner
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,

Just trying to turn my Caffeine Calculator into a Swing program. I'd like it to take input with a dialog (a double) and return whether that double is below or above a value using an if statement.

Just started on Swing so very greenhorn here! Any help with this would be appreciated.

 
Adam Turner
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've edited it and I'm almost finished - why does my if statement not produce the desired ("You drank over...") if I have that as an else statement?

package Messing;

 
Campbell Ritchie
Marshal
Posts: 55785
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know. If you are not getting the code running as expected, you need to take the GUI off. You sh‍ould never create a “GUI app”. You sh‍ould create an application and then write a GUI to support it. If your code doesn't work at the terminal/command line, you need to sort that out first. Make sure your application has an API interface which your GUI can access. You would expect to have methods like setHowManyCups and getHowLongAwake exposed. Then the GUI code can simply pass the information to those methods and Bob's your Uncle.
 
Liutauras Vilda
Marshal
Posts: 4665
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once that stage reached, think what would be shown if user had exactly 5 cups.
 
Paul Clapham
Sheriff
Posts: 22521
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you creating new JLabel objects instead of just calling setText() on the existing ones, the ones which are actually displayed in your GUI?
 
Carey Brown
Bartender
Posts: 3011
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have 3 JLabel's, of which only one may get initialized, the others will remain null. You only need 1 label and call setText() on it to display the results.

You are mixing the display layout with control logic, this is ill-advised. Have one method to layout the display and then use events to modify the items in the display. You already have a text field for input, you'll need some way to have the user let the program know that the input is ready to be processed, perhaps a "submit" button. Then on the event handler for the button, perform the business logic and update the applicable label(s).
 
Adam Turner
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone tell me what code to use? I am totally new at this.
 
Carey Brown
Bartender
Posts: 3011
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've made a good start. This code is 85% yours with some tweaks. Here a button is being created and an action is being assigned to the button. The action is where your business logic should go. I tend to make actions their own inner classes and some people prefer anonymous classes. You no longer need the option pane.

You still need to work on your business logic; there's a bug. There's no error handling.

 
Adam Turner
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much Carey, much appreciated.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!