Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Swing building a small app  RSS feed

 
Serban Cameron
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I would like to build a frame in wich you enter km and converts to miles. I will put the code here but it doesn`t work it gives me some errors and I don`t know why. I am a begginer. Thank you

 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

If you are not experienced in GUIs, you will find the automated GUI builders make the code harder for you to understand. Do you have an application which converts km to miles and vice versa? If not, you will find things difficult. You need an application which you can pass km to and get miles or vice versa. Then you consider putting a GUI around that application.
Miles is km / 1.6 not km * 1.6. Well, actually, 1.609344.

Put [code=java] before the code and [/code] after it. More details here. I have corrected it for you
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't read through all the code but right off the bat this is getting you into trouble:
You have declared a String variable 'km' but not initialized it so therefore it will be 'null'. parseInt() expects a String that represents a number. It will throw an exception when given 'null' or any String that is not a number.
 
Serban Cameron
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply`s. My app code would be simple



And I would like to put this in GUI form but I can`t get the text from a TextField. If you have any ideea please help.

Have a nice day. Thanks
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That does not count as an app which converts between miles and kilometres, not even if you had the right formula. Your application needs a public interface taking values and returning values.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is rarely correct to extend JFrame, unless you have a compelling reason to also override some public or protected method of JFrame.
Have your class extend JPanel instead, and just create a Jframe and add your class to it.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably don't need to extend the panel either, since you haven't overridden a paintXXX method.
Usually the only method you need to override in a panel is paintComponent, which by the way shou‍ld have protected access.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!