sets the text of the 'answer box' (so to say) with the first inputed value and the second inputed value - as it calls the methods to preform the task Therefor I assume this was my fault in the 'AWT' program. I made an action listner however I did not create two 'get inputed value' commands (or methods in this case).
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
code:
public int getFirstInputValue()
{
return Integer.parseInt(firstInputValue.getText());
}
The 'getText()' method call. Clarification is needed on my part () would I have to create this 'getText' method or is it a prebuilt method within javax.swing?
Sponsor Sponsor
wtd
Posted: Sun Jan 01, 2006 8:30 pm Post subject: (No subject)
If you could show us all of your code, that would be helpful.
xLockx
Posted: Sun Jan 01, 2006 9:45 pm Post subject: (No 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.
code:
import javax.swing.*;
import java.awt.event.*;
public class FunMath2 extends JApplet
{
//Instead of using straight AWT - this program uses String.
//The Difference is instead of declaring firstInputLabel = new Label; its 'JLabel' firstInputLabel;
private JLabel firstInputLabel;
private JLabel secondInputLabel;
private JLabel answerLabel;
//Instead of 'firstInputField = new TextField; its 'JTextField firstInputField;
private JTextField firstInputField;
private JTextField secondInputField;
private JTextField answerField;
//Instead of 'addButton = new Button; its 'JButton addButton;'
private JButton addButton;
private JButton subtractButton;
private JButton multiplyButton;
private JButton divideButton;
public void init()
{
//Inputs the 'labels' basically the 'System.out.printlns of applets'.
//instead of firstInputLabel = new Label("First"); its firstInputLabel = new JLabel("First");(Not a big differece)
firstInputLabel = new JLabel("First");
secondInputLabel = new JLabel("Second");
answerLabel = new JLabel("Answer");
//Difference noted above 'J'
firstInputField = new JTextField();
secondInputField = new JTextField();
answerField = new JTextField();
//Same thing 'J'
addButton = new JButton("Add");
subtractButton = new JButton("Subtract");
multiplyButton = new JButton("Multiply");
divideButton = new JButton("Divide");
/*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.*/
//Instead of creating a new action(Event e, Object o) method its using simple commands.
addButton.addActionListener(new ActionListener());
{
public void actionPerformed(ActionEvent e)
{
answerLabel.setText(Integer.toString(getFirstInputValue() + getSecondInputValue()));
}
}//end of add button.
subtractButton.subtractActionListener(new ActionListener());
{
public void actionPerformed(ActionEvent e)
//block for action preformed
{
answerLabel.setText(Integer.toString(getFirstInputValue() - getSecondInputValue()));
}
}//end of subtract button.
multiplyButton.multiplyActionListener(new ActionListener());
{
public void actionPerformed(ActionEvent e)
{
answerLabel.setText(Integer.toString(getFirstInputValue() * getSecondInputValue()));
}
}//end of multiply button.
divideButton.divideActionListener(new ActionListener());
{
public void actionPerformed(ActionEvent e)
{
answerLabel.setText(Integer.toString(getFirstInputValue() / getSecondInputValue()));
}
}//end of multiply button.
public int getFirstInputValue()
{
return Integer.parseInt(firstInputValue.getText());
}
public int getSecondInputValue()
{
return Integer.parseInt(secondInputValue.getText());
}
}
}
I still cannot figure out why the code reads 'illegal start of expression' with respect to
code:
public void actionPerformed(ActionEvent e)
in this section of the program (same for subtract, multiply & divide.) The only explination I can come up with would be the fact that its a method creation blocked within a action Listner (as I am quite sure one cannot create a second method, within a creation of the first method)
public int getFirstInputValue()
{
return Integer.parseInt(firstInputValue.getText());
}
the method call does that getText() method have to be created, or is it a preset method for Swing?(for example like setText() ?)
xLockx
Posted: Tue Jan 03, 2006 1:28 am Post subject: (No 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... )
code:
import javax.swing.*;
import java.awt.event.*;
public class FunMath2 extends JApplet
{
//Instead of using straight AWT - this program uses String.
//The Difference is instead of declaring firstInputLabel = new Label; its 'JLabel' firstInputLabel;
private JLabel firstInputLabel;
private JLabel secondInputLabel;
private JLabel answerLabel;
//Instead of 'firstInputField = new TextField; its 'JTextField firstInputField;
private JTextField firstInputField;
private JTextField secondInputField;
private JTextField answerField;
//Instead of 'addButton = new Button; its 'JButton addButton;'
private JButton addButton;
private JButton subtractButton;
private JButton multiplyButton;
private JButton divideButton;
public void init()
{
//Inputs the 'labels' basically the 'System.out.printlns of applets'.
//instead of firstInputLabel = new Label("First"); its firstInputLabel = new JLabel("First");(Not a big differece)
firstInputLabel = new JLabel("First");
secondInputLabel = new JLabel("Second");
answerLabel = new JLabel("Answer");
//Difference noted above 'J'
firstInputField = new JTextField();
secondInputField = new JTextField();
answerField = new JTextField();
//Same thing 'J'
addButton = new JButton("Add");
subtractButton = new JButton("Subtract");
multiplyButton = new JButton("Multiply");
divideButton = new JButton("Divide");
/*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.*/
//Instead of creating a new action(Event e, Object o) method its using simple commands.
addButton.addActionListener(new ActionListener()//MUST LEAVE OPEN *** Cannot ');'
//as it must be 'closed' after the Action Preformed
{
public void actionPerformed(ActionEvent e)
{
answerLabel.setText(Integer.toString(getFirstInputValue() + getSecondInputValue()));
}
});
//button COMMAND Must leave as addActionListner!
subtractButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
//block for action preformed
{
answerLabel.setText(Integer.toString(getFirstInputValue() - getSecondInputValue()));
}
});
divideButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
answerLabel.setText(Integer.toString(getFirstInputValue() / getSecondInputValue()));
}
});
}
public int getFirstInputValue()
{ //NOT input Value input field!Watch Variable names!
return Integer.parseInt(firstInputField.getText());
}
public int getSecondInputValue()
{
return Integer.parseInt(secondInputField.getText());
}
}
McKenzie
Posted: Tue Jan 03, 2006 11:11 am Post subject: (No 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:
code:
add(firstInputLabel);
at the bottom of your init.
xLockx
Posted: Wed Jan 04, 2006 1:30 am Post subject: (No subject)
Good call McKenzie - I can't believe I left those out.. *sigh*
Program is official up and running, thanks for the help 'wtd' and Mckenzie - appreciated it.