This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
My image isn't showing up as a label on my GUI business. Do I have something in my code overriding it? Did I code it wrong? My other program with an image as a label works fine. I have verified that I'm using the correct file, and file extension, and that it is in the correct location. As far as research is concerned, I've been reading about ImageIcon to see if there is anything that clashes with it, as all the book does is skim (at best) over graphics in GUI's. Am I even looking in the right place? Thanks in advance for any help/advice you can offer.
I am afraid you will have to provide more details. Where is the image file? Can you load it? Which component is it supposed to be displayed on? Have you used print statements to get debugging information?
I had to edit your code because it has long lines in, which are too wide for a screen.
All sorts of things about your code, I am afraid.
1: Even without reading all your code, I think you have a God class. One that is almighty and tries to do everything. Not usually a good idea at all.
2: You have all sorts of local variables in the main method which appears never to be used; they simply go out of scope.
3: You have lots of fields in the recipe class. I suspect most of them can be local variables in the code which sets up the GUI.
4: Another thing you see in some books, addActionListener(this). That is liable to lead to awkward, non-object-oriented programming. Create your listeners as separate classes.
5: You are using the == operator on reference types (== "Tacos"). That is very unreliable; you should use an equal...() method.
I am pleased to see you are not making your principal class extend frame or similar, and that you are using invokeLater.
I think this discussion would fit better in our GUIs forum, so shall move it.
Joined: Feb 29, 2012
Thanks! I'm super new (obviously). You bring up some really great points. I'll work on the stuff you pointed out first, and then come back if my question is still relevant. BTW this place is awesome!
Joined: Oct 13, 2005
How have you got on with it?
Joined: Feb 29, 2012
Well, what happened was I wrote the program at home implementing new stuff I learned at school, so every time I learned something (i.e. if-else statements) I would add it to the program. The problem with that is when I learned about using swing and GUI business I didn't know what to do with the rest of my code. So I just commented it out for now, which is why I had all those extra variables. As far as the God class problem is concerned, I've been thinking about how to break it down into separate classes and methods, but not sure where to start. I changed all the == to .equals, thank you for the heads up. I'm trying to figure out how to create my listeners as separate classes, but to do that I think I need to really understand what a listener is first. My book has a paragraph on it, so I think I need to read more on the internet. This place is soo much nicer than the other forum I was introduced to, thanks again for all your help.