• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Swing building a small app

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic