Computer Science Canada Having two WORKING inputs for a applet |
Author: | xLockx [ Fri Dec 30, 2005 1:40 am ] |
Post subject: | Having two WORKING inputs for a applet |
Hey Guys - Here with another question im quite stumped... So heres my dilema I wanted to variables to add, subtract, multiply or divide depending on the buttons pushed. I got it all down pat for one variable but when I added two variables 'to the mix' it got weird on me. I thought just adding some labels, boxes, and duplicate code should handle it - as im just passing 2 variables and returning one variable a method ( I know its only one variable because a method can only return one). So the program runs except it completely disregards the second variable. Any Ideas? <Beggining of CODE> // THE "FunMath" CLASS //Thanks in Advance Compsci guys (and girls?) for all your help! //Programmed by - Nick B. import java.applet.Applet; import java.awt.*; //Class to output bus fare given age. public class FunMath extends Applet { Label prompt,prompt2,result;//System.out.printlns of applets ![]() TextField input,input2,answer;//Fields for user nput. int var1,var2;//Intigers that the user types in. Button adds; Button subtract; Button multiply; Button divide; // sets up GUI components (just a reminder of what init is) public void init() { prompt = new Label ("Type in first Variable"); input = new TextField (1); prompt2 = new Label ("Type in second Variable"); input2 = new TextField (5); //Buttons for basic mathamatical functions adds = new Button("Add"); subtract = new Button("Subtract"); multiply = new Button("Multiply"); divide= new Button("Divide"); result = new Label ("The result is "); answer = new TextField (15); // Place label and text field on applet add(prompt); add(input); add(prompt2); add(input2); add(result); add(answer); add(adds); add(subtract); add(multiply); add(divide); }//init method //Respond to action of user's input public boolean action(Event e, Object o) { //Now this is the part I THINK is messing with my program. //Basically its always reading the first variable but completely disregard //The second inputed variable and assigns it to 0.(like it was never entered). var1 = Integer.parseInt(input.getText ()); var2 = Integer.parseInt(input.getText ()); if(e.target instanceof Button) { if(e.target == adds) answer.setText (""+ Integer.toString (adds(var1,var2)));//ANSWER.SETTEXT its the BOX(side note for me to remeber what it means) else if (e.target == subtract) answer.setText ("" + Integer.toString(subtract(var1,var2))); else if (e.target == multiply) answer.setText (""+ Integer.toString (multiply(var1,var2))); else if (e.target == divide) answer.setText ("" + Integer.toString(divide(var1,var2))); return true; } return true; } public int adds (int number, int number2) { return (number+number2); }//adds public int subtract (int number,int number2) { return (number-number2); }//subtract public int multiply (int number,int number2) { return (number*number2); }//multiply public int divide (int number,int number2) { return (number/number2); }//divide }//end of Fun with Math class </Beggining of CODE> |
Author: | xLockx [ Fri Dec 30, 2005 1:42 am ] |
Post subject: | |
Ah Darnit! I indented everything but for some reason the Copy and Paste didn't work - sorry guys - I know its supposed to be indented - and I did it properly :p but... ya didnt turn out that way apperently |
Author: | wtd [ Fri Dec 30, 2005 1:54 am ] |
Post subject: | |
You need to use code tags. |
Author: | xLockx [ Fri Dec 30, 2005 2:06 am ] |
Post subject: | |
I'm sorry... what is a code tag? |
Author: | wtd [ Fri Dec 30, 2005 3:00 am ] | ||
Post subject: | |||
|
Author: | xLockx [ Fri Dec 30, 2005 12:56 pm ] | ||
Post subject: | |||
My bad ![]()
|
Author: | wtd [ Fri Dec 30, 2005 2:40 pm ] |
Post subject: | |
Which version of Java are you using? I ask because you're using straight AWT rather than Swing, which I think is a bad idea. Additionally, if you look up the documentation of the "action" method, it has been deprecated since JDK 1.1. You would be much better off using proper ActionListeners. |
Author: | xLockx [ Fri Dec 30, 2005 3:36 pm ] |
Post subject: | |
the reason why I am using action is because it is what I was taught - Action listeners do sound like a better idea - any ideas where I can learn about them? is it the same general concept... ect..? If I use action listeners would it correct my problem, I think it would.... I think the problem with my program is the fact it is simply 'identifying' the first input as var1, and not assinging a variable to var2 simply because it already 'picked up' / 'assigned a value'... Any suggestions with that regard - am I totally off base? :p In regards to Java, I use JGrasp as my editor and - as far as I know the ladest version of Java Development Kit from www.java.sun.com. |
Author: | wtd [ Fri Dec 30, 2005 5:25 pm ] | ||
Post subject: | |||
You could make your code work as it is now. Try registering the object as its own action listener with:
Added to the "init" method. |
Author: | xLockx [ Fri Dec 30, 2005 6:18 pm ] | ||||||||
Post subject: | |||||||||
Ok so you say
Thanks again for your help (in advanced of course) |
Author: | wtd [ Fri Dec 30, 2005 6:32 pm ] |
Post subject: | |
No. Try the code exactly as I wrote it. |
Author: | xLockx [ Fri Dec 30, 2005 6:48 pm ] | ||
Post subject: | |||
Yeilds 'FunMath.java:19: cannot find symbol' Perhaps this is occuring because I only imported 'java.applet.Applet; & ava.awt.*;' I tried importing 'javax.swing.*; but it had no effect. |
Author: | wtd [ Fri Dec 30, 2005 6:57 pm ] | ||
Post subject: | |||
Here's an imcomplete example of using Swing.
|
Author: | xLockx [ Sat Dec 31, 2005 1:19 am ] | ||||||
Post subject: | |||||||
Just need a bit of clarifcation - I'll write out what I FIRST had troubles with (because I only have used the Javax.swing.*; once before (really without knowing using JOptionPane)). (Perhaps others will read this post later or now - and have a bit of trouble and you can learn from my confusion) So as many can see the Difference is (for example) instead of declaring firstInputLabel = new Label; its 'JLabel' firstInputLabel;(when using 'Swing' class). Also similar for the initiate class - instead of firstInputLabel = new Label("First"); its firstInputLabel = new JLabel("First");(Not a big differece) However the 'new' 'Swing' (in comparison to using AWT) Instead of having to add all the 'variables' - labels and buttons to the Applet the 'J' String class already does that. MUCH like JOption pane the AWT is actually just OptionPane but when using 'javax.swing.*;' it makes everything alot eaiser - as it automatically applies everythign without having to add(x) it. (im hoping that explination made sense) The main improvements to the program is using 'Action Listeners' instead of having to use the method
There is also a change - that I did not notice - perhaps anyone else reading (or mabey just me - but I was stumped) :p Some coders prefer to put their 'curly brackets' or '{ & }' right after a 'command' instead of on a new line ( I was first introduced to that today - with this program 'wtd' made - with a bit of 'research' I found it was quite common..) and what stumped me the most was why
Anywho - to the main reason why im writing this post - when I added the following to complete the program (because 'wtd' started me of so well and nudged me in the right direction) it came up with a nice 'illegal start of expression 'error at compile... Now it may be me overlooking a simple objection but perhaps you could give it a look?
Thanks again 'wtd' for your quick aid and guidance! |
Author: | wtd [ Sat Dec 31, 2005 1:45 am ] | ||||||
Post subject: | |||||||
Of course this causes errors. It's not valid Java code.
Creates an "anonymous inner class". The ActionListener interface specifies that the actionPerformed method has to be defined. That is what we do within the block.
Is the proper form. And get rid of that trailing comment. It's bad form. |
Author: | xLockx [ Sun Jan 01, 2006 8:12 pm ] | ||||||
Post subject: | |||||||
I understand now that the actionPerformed method has to be defined, in order for the action listener to correctly opperate. However would this correctly 'store' both inputed values from their seperate 'Text Fields'. I see that
However - i'm still stumped as to how I would intigrate this peice of code
into the program, as it still reads 'illegal start of expression' and I am at a loss for the solution. Secondly I just recently noticed within
The 'getText()' method call. Clarification is needed on my part ( ![]() |
Author: | wtd [ Sun Jan 01, 2006 8:30 pm ] |
Post subject: | |
If you could show us all of your code, that would be helpful. |
Author: | xLockx [ Sun Jan 01, 2006 9:45 pm ] | ||||||||
Post subject: | |||||||||
Oh - pardon me, I do suppose that would clarify things... You will notice wtd that my code - well is basically the code you wrote out, although I did add comments that help me remeber what each line / section means...As well I do know, some of these comments are redendant or not needed however it helps me visualize when different sections end, and others begin, that is why I have alot of comments throughout. In the final version I do 'sift' through the program and delete all unnessesary comments, but when I am still 'coding' I like to have a full description of each line, if I forget the next time I go to code.i'll restate the problems I am having at the end of this post.
I still cannot figure out why the code reads 'illegal start of expression' with respect to
Secondly I have a general question in
|
Author: | xLockx [ Tue Jan 03, 2006 1:28 am ] | ||
Post subject: | |||
Wow... What an error I have made ... So.. lets review wtd flat out gave me the answer :p and I asked it again, I assumed that putting the ');' after the action preformed was simply like adding curly brakets ({ or }) at the beggining of a new line or leaving on the same line... well I was obviously wrong.. I come to a error once again with my program and this time... it has befuddeled me... its a blank screen when I load the applet.... any suggestions (the updated code follows) (Thanks again... ) ![]()
|
Author: | McKenzie [ Tue Jan 03, 2006 11:11 am ] | ||
Post subject: | |||
In the code given you haven't actually added any of your components to your form. To make your form look right you will need to use layout managers (or absolute positioning) but just to get them on the form use add. like:
at the bottom of your init. |
Author: | xLockx [ Wed Jan 04, 2006 1:30 am ] |
Post subject: | |
Good call McKenzie - I can't believe I left those out.. ![]() Program is official up and running, thanks for the help 'wtd' and Mckenzie - appreciated it. |