Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

GUI integration with code written

 
Michael Humphreys
Greenhorn
Posts: 23
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have used net beans to build a GUI for my program. It works great but when i incorporated
my already written code I get "variable not used" errors on the variables in the already written code. I am doing something wrong but I don't know what. I also don't know how to use the variables in the GUI code. The lines I am having a problem with is line 31 to 44. Any help would be appreciated.

Here is a piece of code that I am having the problem with.



Again thanks.
 
Geoff Jefferson
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have declared many members (variables).
Have you assigned anything to them?

Take a look at:

http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html

G.

 
Geoff Jefferson
Ranch Hand
Posts: 102
 
Michael Humphreys
Greenhorn
Posts: 23
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Geoff Jefferson wrote:You have declared many members (variables).
Have you assigned anything to them?

Take a look at:

http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html

G.


Thanks for the link i am reading it, but I don't understand how to assign the input values to my variables
I see where the text fields are initialized but how do i get the info to the right place? Also I don't know how to make the submit button to run the program.
Thanks again.
 
Michael Humphreys
Greenhorn
Posts: 23
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
The method and class you see is an inner class so it is not static.

Here is the code:

When I run my program i get this as the output:

java.lang.NoClassDefFoundError: eggsprofitandloss/Main
Caused by: java.lang.ClassNotFoundException: eggsprofitandloss.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: eggsprofitandloss.Main. Program will exit.
Exception in thread "main" Java Result: 1

If anyone could point me in the right direction i would find that most helpful.

Michael

 
Campbell Ritchie
Marshal
Posts: 52630
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Humphreys wrote: . . . I have used net beans to build a GUI for my program. . . .
If you are a beginner, that is a serious mistake. Write all your code by hand until you are used to writing GUI code. That is why you have those poorly-named fields. Why are those fields of the class in the first place? You can probably make them all local variables in your GUI constructor or setUpGUI method. Beware of NetBeans here; if you try to change those fields, it will simply change the list back

I think you need a FowlFarm object. Give it things like priceOfEggs numberOfChickens and priceOfFeed as fields, with set and get methods. Give it calcfulateCosts and calculateProfits methods. You should already know how to do that; the logic is identical to your old class.
Get that working.

You beginners have a dreadful habit of trying to do everything at once. It doesn't work like that. You get bogged down with lots of things going wrong, and confusion about inner classes (which is yoiur inner class? why have you got inner classes in the first place?). Do it a little bit at a time. You should be able to write this sort of class in an hour, and have it running (remember all the logic you want is already written in your older eggs classes)Note you put all the instructions like setEggPrice, setFeedPrice into the go method. Think of a better name for it than go.
Then when you get into your GUI, you can have a listener whose method reads a bit like thisNote: if you have the text fields as local variables, they must be declared as final
 
Michael Humphreys
Greenhorn
Posts: 23
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,
I have read your comments and wanted to ask a question about some things that you said. You suggested that as a beginner I should bite off smaller bites. The way I set up my studies I started with a basic program then added to it progressively until I reached the point I am at now. The question is should I have done things differently? Is my studies progression flawed? And thank you Campbell I appreciate the comments.

Michael
 
Campbell Ritchie
Marshal
Posts: 52630
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Humphreys wrote: . . . I should bite off smaller bites. The way I set up my studies I started with a basic program then added to it progressively until I reached the point I am at now. . . .
No, you haven't. You have written 67 lines of GUI code (or more precisely, told an IDE to write 67 lines). It has at present no connection to your old non-GUI code, which did actually work. You still have a few things wrong, eg using == true and yield - cost < 0.00. The first should be omitted altogether, and latter should read yield < cost. Alternatively, you should work out a profit variable and then test whether Math.abs(profit) < 0.005 or profit < 0, etc. Then you can print profit to screen. You also seem to have incorrect indentation, and a cost local variable which you never use.
For your GUI to work, you need to get that into object-oriented form. You need to be able to saySome of those methods have void return type, the latter two return something real, which you can use in your GUI. The void methods may take input; you might have a "Chickens Bought" field where you enter the number of chickens bought to augment your farm, or a "Chickens Missing" field where you enter how many have died strayed or been eaten by foxes.
As I said before, you can set up a class which will do that in an hour, and have it working in a non-GUI environment in an hour. I told you already, you have the necessary logic in your old class already.
 
Campbell Ritchie
Marshal
Posts: 52630
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . then, you start with buttons which copy the values from your input fields on your GUI into the class, or call the sellAllEggs methods, or similar.
When I said about bit by bit, I meant that you write one method, compile and run the application, and only when it is working, think of writing a second method. You may need some additional code for temporary use, like thisNow you have written 5 lines of code; if you find a nasty error you only have 5 lines to blame that error on. If you had written 25 lines and tried to run the application, it would have been that much more difficult to find the error.
 
Paper has not yet been outlawed – so let your Java Swing application print with wild abandon: JPDF
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!