No, sorry. We are only too pleased to help people with problems with their code, not give them our code.
What you need to do is
Save your work in its present form, in case anything goes wrong and you have to go back and start again.get a testCalculation methodPut something in it which reads the price from the JTextFieldsAdd an ActionListener to one of your buttons which calls the testCalculation methodParse any Strings to numbers as requiredCheck for incorrect numbers (NumberFormatException)Multiply "price" by "count" and add them all up.Print the whole working to System.out, so you can see what is happeningIf you are using an IDE, refactor the name of testCalculation to calculateTotalPrice or similar. I shall fulminate about action listeners here.
Lots of books, and the
Java Tutorial
use "addActionListener(this)"
passim. A violation of the principles of object-oriented programming, because you end up with an enormous method somewhere full of nasty things like this
Now try to add button 6.
A total mess. The only instance where addActionListener(this) is where the event directly affects the Component. A MouseListener where you display the x y coordinates of the last click, or a WindowListener which does something when the user tries to close the window, are good examples.
I have a simple rule of thumb, which tells me that you need to decide whether there is any chance of using the same Listener more than once.
Are you going to use the same action or something similar more than once? A class which implements the Listener.Are you going to use the same action or something similar and reuse it in different classes? A public class which implements the Listener.Are you going to apply this Listener to one Component and one Component only, and not use anything similar anywhere else? An anomymous inner class.There is also the option of using an Adapter class, but I don't like them; the tiniest spelling error can produce a Listener which doesn't work and you can't see why. You can also create an Action and add it to the button, but I have never tried that. It tells you about Actions in the Java Tutorial
here. Look up
the Java Tutorial about Listeners.Look up
the EventListener interface, then find some of its subinterfaces; for a button you want ActionListener. Read the method description carefully.
Decide which class type you want;
Public class, usable by any app you program:
If you think you might not use it outside your present class, put the ColourListener inside the class and change the "public class" declaration to "private class"
If you have one Listener and one Listener only and you are not using it (or anything similar on more than one Component, use the anonymous inner class. Don't use anonymous inner classes for "exitListener" if you have two "exit" components (eg exitButton, exitMenuItem) because you don't want to use the same code twice.
If you are using an IDE, you can probably train it to write an anonymous inner class for you. If you are doing it by hand, I suggest you do it in these stages. Note it won't compile in the earlier stages.
1: Decide which listener you are adding to which Component. invoke its addWhatever method:
2: Make space for it. Put your cursor in the () and push "enter" twice.
3: Put an instantiaton statement in the gap, and "enter":
4: Convert your interface to a class with a pair of braces:
5: Put the method heading(s) inside the class body (you can copy them from the API for whichever listener you are using):
6: Put the braces and "enters" in to make the method body:
At this point you can compile the code, but it won't do anything.
7: Put something more sensible than this inside the method:
8: Remember your Javadoc comments
If you implement a listener with severel methods and don't need them all, don't simply leave a blank method; write "/* empty method */". Then you won't think you have forgotten anything later on.
If your methods inside the anonymous class use any local variables or parameters of the surrounding block (constructor or setUpGUI method), then you have to go and declare those variables or parameters "final".
Yes, I know it sounds complicated, but it does work.
CR
[edit]Bit of text in wrong location moved, and some spelling corrections[/edit]
[ April 20, 2007: Message edited by: Campbell Ritchie ]