-
GUI Calculator: Adding Answer Button? Please Help
Code Java:
public class event implements ActionListener{
public void actionPerformed(ActionEvent operations){
String check = operations.getActionCommand();
if(check.equals("Add")){
try{
field1.setText("");
field2.setText("");
field3.setText("");
popup.setText("" + trademark);
double num1, num2, answer;
field2.setVisible(false);
mainprompt.setText("Enter Numbers Below");
prompt1.setText("Number");
prompt2.setText("+");
prompt3.setText("Number");
num1 = Double.parseDouble(field1.getText());
num2 = Double.parseDouble(field3.getText());
answer = num1 + num2;
popup.setText("The sum of " + num1 + " and " + num2 + " is " + answer + ".");
mainprompt.setText("Choose Operation Above");
//Reset Fields Once Answer is Received
prompt1.setText("Number");
prompt2.setText("Number");
prompt3.setText("Number");
field1.setVisible(true);
field2.setVisible(true);
field3.setVisible(true);
field1.setText("");
field2.setText("");
field3.setText("");
}catch(Exception e){
//Fixes another error
prompt1.setText("Number");
prompt2.setText("+");
prompt3.setText("Number");
field1.setVisible(true);
field2.setVisible(false);
field3.setVisible(true);
field1.setText("");
field2.setText("");
field3.setText("");
}
}
}
}
This portion of code adds functionality to my "Add" button. I have an "Answer" button created, that currently does nothing. I'm trying to place that "Answer" button within the if statement so that if the user clicks the button, it will return (popup.setText("The sum of " + num1 + " and " + num2 + " is " + answer + ".")).
If more code is needed, I'll put up my condensed version of my calculator (183 lines).
-
Re: GUI Calculator: Adding Answer Button? Please Help
What's your question? What have you tried?
-
Re: GUI Calculator: Adding Answer Button? Please Help
Question: How can I add a button, INSIDE the 'if' statement, that will set the text of a JLabel to the answer.
Attempts:
1. I've attempted to add an 'if(check.equals("Answer")) statement within the 'if(check.equals("Add"))' statement however, the button's functions act like a reset button (reset all labels, and textfields).
2. I've also attempted to make an ''if' statement outside the 'if(check.equals("Add"))' statement and simply setting the text to an answer once the button is clicked, but the answer variable is not recognized within this if statement.
3. Lastly, I've made this program work by using JOptionPane, however I don't want a popup to reveal the answer.. I would like to have my answer displayed within the same window.
-
Re: GUI Calculator: Adding Answer Button? Please Help
Sorry, I'm confused- why do you want to add a JButton inside the if statement? Do you mean you want to set the text of a JButton? Or do you mean you want to add the ActionListener to another JButton?
-
Re: GUI Calculator: Adding Answer Button? Please Help
I completely worded my question wrong..
I already have an "Answer" button created. It has an ActionListener as well.. Let me show you how my program works so far.
User clicks JButton "Add" -> 'Program sets two fields visible for user input' -> User enters numbers into both fields -> User clicks JButton answer -> Nothing Happens.
I want that JButton "Answer" that I have created to set the text of one of my labels, to the answer.
Sorry, I'm really trying to make this clear.
So, knowing I have an Answer Button with an ActionListener, and I also have a JLabel created to show the answer.. My question is. How do I make that Answer Button set the JLabel's text to the answer.
-
Re: GUI Calculator: Adding Answer Button? Please Help
Quote:
I want that JButton "Answer" that I have created to set the text of one of my labels, to the answer.
Add an action listener to the Answer button. In that listener's actionPerformed method, do what you want done when the button is pressed.
Quote:
"Answer" button created. It has an ActionListener as well
What is done in that listener? Why not set the text there?
-
Re: GUI Calculator: Adding Answer Button? Please Help
Quote:
Originally Posted by
Norm
Add an action listener to the Answer button. In that listener's actionPerformed method, do what you want done when the button is pressed.
So create a new Method specifically for the Answer button?
Quote:
Originally Posted by Norm
What is done in that listener? Why not set the text there?
Because in my If Statement for my Add Button, I have the variable, answer, that cannot be identified in the If Statement for the Answer Button. (My Terminology may be off)
-
Re: GUI Calculator: Adding Answer Button? Please Help
Quote:
So create a new Method specifically for the Answer button?
Not a method, a new listener class.
You will need a reference to the answer variable if you want to use its value.
If answer is a class reference you can pass it to the constructor for the listener so that the listener can call its methods.
-
Re: GUI Calculator: Adding Answer Button? Please Help
Quote:
Originally Posted by
Norm
If answer is a class reference you can pass it to the constructor for the listener so that the listener can call its methods.
Sorry, but I don't entirely understand this. It is only my 4th day learning Java.
-
Re: GUI Calculator: Adding Answer Button? Please Help
Passing a value to a method or constructor:
objRef.method(answer); // pass answer to the method
AClass aClass = new AClass(answer); // pass answer to the constructor
Example of the Constructor:
Code :
class AClass implements ActionListener {
<answer's data type here> answer; // define class variable
public AClass(<answer's data type here> answer) { // the constructor
this.answer = answer; // save the value for use later
...
// answer can be used by any method in this class
}
-
Re: GUI Calculator: Adding Answer Button? Please Help
Thank you very much Norm. However, this did not work for me. I found a less appealing, but simpler way to return the answer... The Program runs like so..
User clicks JButton "Add" -> 'Program sets two fields visible for user input' -> User enters numbers into both fields -> User clicks JButton "Add" -> JLabel reveals answer.
This is the original way I made my program to run. I'm completely okay with this, but the idea of an Answer Button is still enticing.. If you want to keep working with me, to try and solve this, that'd be fantastic. But if not, thanks for everything!
-
Re: GUI Calculator: Adding Answer Button? Please Help
As you gain experience you'll be able to create new classes as needed.
-
Re: GUI Calculator: Adding Answer Button? Please Help
Quote:
Originally Posted by
Staticity
Thank you very much Norm. However, this did not work for me. I found a less appealing, but simpler way to return the answer... The Program runs like so..
User clicks JButton "Add" -> 'Program sets two fields visible for user input' -> User enters numbers into both fields -> User clicks JButton "Add" -> JLabel reveals answer.
This is the original way I made my program to run. I'm completely okay with this, but the idea of an Answer Button is still enticing.. If you want to keep working with me, to try and solve this, that'd be fantastic. But if not, thanks for everything!
All you really need is an inner class that implements ActionListener, add the actionPerformed method, and within it, obtain the user's inputs and display the answer. Example:
Code java:
[CODE]
import java.awt.event.*;
import javax.swing.*;
public class Calculator {
//GUI code... blah blah blah
JButton answer = new JButton("Solve");
answer.addActionListener(new ListenForSolve());
//more code..
class ListenForSolve implements ActionListener {
//note it's still within class Calculator's body...
public void actionPerformed(ActionEvent e) {
//Obtain user input fields (probably through parsing the contents of a JTextField?)
//Process inputs (a+b=c)
//Set JLabel to display answer or set another field/call a method to do so.
}
} //end of ListenForSolve body
} // end of Calculator body
[/CODE]
-
Re: GUI Calculator: Adding Answer Button? Please Help
Quote:
Originally Posted by
bgroenks96
All you really need is an inner class that implements ActionListener, add the actionPerformed method, and within it, obtain the user's inputs and display the answer. Example:
Thanks, but I already know all of that. That's how my whole Calculator works. Just for some odd reason, I can't find a way to set the Answer Button's functions... I don't know how to explain it any further..
Quote:
Originally Posted by Norm
As you gain experience you'll be able to create new classes as needed
I surely hope so. Time will only tell, and again.. Thanks.
-
Re: GUI Calculator: Adding Answer Button? Please Help
Quote:
Just for some odd reason
That sounds like an error message from a compiler.
-
Re: GUI Calculator: Adding Answer Button? Please Help
Quote:
Originally Posted by
Staticity
Just for some odd reason, I can't find a way to set the Answer Button's functions... I don't know how to explain it any further..
IIUIC, the Answer button's function is to set some text in a text component. The ActionListener's actionPerformed(..) method is what should do this. If you make the ActionListener an inner class (i.e. declare it inside the class that contains the text component and the button), as bgroenks96 suggested, it will have direct access to that class's member variables, including the text component, so it will be able to set the text into it directly.
-
Re: GUI Calculator: Adding Answer Button? Please Help
Quote:
Originally Posted by
dlorde
IIUIC, the Answer button's function is to set some text in a text component. The ActionListener's actionPerformed(..) method is what should do this. If you make the ActionListener an inner class (i.e. declare it inside the class that contains the text component and the button), as bgroenks96 suggested, it will have direct access to that class's member variables, including the text component, so it will be able to set the text into it directly.
Here is an example of what I need to do. I'm going to make up something...
Code Java:
import javax.swing.*;
public class test{
public static void main(String args[]){
JButton button1 = new JButton("Bob");
JButton button2 = new JButton("Sarah");
JButton button3 = new JButton("");
if(button2.equals("Sarah")){
button3.setText("" + answer);
}
if(button1.equals("Bob"));
double num1, num2, answer;
num1 = 1;
num2 = 2;
answer = num1 + num2;
}
}
^^^
I need to do that within the Inner Class that implements ActionListener. Or that is what I am attempting to do. I need a way of putting that answer variable within that first if statement.
-
Re: GUI Calculator: Adding Answer Button? Please Help
You need to move ALL of the component definitions OUT of the main method to the class level.
Nothing that you define in a method will be known outside of the method.
Defining a variable is not the same as assigning it a value.
Code :
class AClass {
Component aComp; // define a variable of type Component
public static void main(String[] args) {
aComp = new Component(); // assign a value to aComp
...
} // end main()
...
} // end class AClass
Comment on your code:
Code :
if(button1.equals("Bob"));
Leave the ending ; off this statement. When you code an if statement, after the condition in ()s immediately add a { at the end (or the next line and a } on the next line after that.
Code :
if(some conditions) {
// Then fill in the stuff to do when true
}
-
Re: GUI Calculator: Adding Answer Button? Please Help
Quote:
Originally Posted by
Norm
You need to move ALL of the component definitions OUT of the main method to the class level.
It was an example. The Main Method is at the bottom of all my code. It's not involved with the class that implements ActionListener.
Quote:
Originally Posted by Norm
Nothing that you define in a method will be known outside of the method.
Defining a variable is not the same as assigning it a value.
I understand the difference between defining a variable and assigning it a value :\ I've just been looking for a way to use the variable inside of one if statement, in another if statement.
-
Re: GUI Calculator: Adding Answer Button? Please Help
Quote:
a way to use the variable inside of one if statement, in another if statement.
Same type of problem dealing with the scope of a variable. You need to define the variable at a level that the code in both if statements can see it.
-
Re: GUI Calculator: Adding Answer Button? Please Help
Quote:
Originally Posted by
Norm
define the variable at a level that the code in both if statements can see it.
That's my predicament. I don't know how to.
-
Re: GUI Calculator: Adding Answer Button? Please Help
If the variable only needs to be known in the method with the if statements, define the variable at the method level: inside of the {}s that hold the method's statements and not inside of an {}s within the method:
Code :
public void aMethod() {
int aMethodVar = 0; // define this variable at the method level
if (cond) {
int x = 0; // define this variable inside of the if{} it goes away at the ending }
// aMethodVar is known here
} // end of if x is now gone
int anotherVar = 33; // define this variable at the method level
if(cond2) {
// anotherVar and aMethodVar are known here. x is NOT known
}
} // end aMethod()
-
Re: GUI Calculator: Adding Answer Button? Please Help
Quote:
Originally Posted by
Norm
If the variable only needs to be known in the method with the if statements, define the variable at the method level: inside of the {}s that hold the method's statements and not inside of an {}s within the method
I also understand this. but I cannot define the variable at method level because the answer variable is bound to the if statements. In my Calculator, I have 9 different operations that the user can choose from.
Here is a Fake Example of my issue.
Code Java:
public void aMethod(){
double answer; //number is never initialized to a value.
if(add.isSelected()){
double num1, num2;
answer = num1 + num2; //answer = num1 + num2.. only in this if statement
}
if(div.isSelected()){
double num1, num2;
answer = num1 / num2; //answer = num1 /num2.. only in this if statement
}
if(quad.isSelected()){
double num1, num2, num3, answer1, answer2; //Quadratic Formula calls for 2 answers(conflicts with method variable)
answer1 = ((-num2)+Math.sqrt((num2*num2)-(4*num1*num3)))/(2);
answer2 = ((-num2)-Math.sqrt((num2*num2)-(4*num1*num3)))/(2);
}
if(answerbutton.isSelected()){
fakelabel.setText("Your answer is" + answer); //answer still has no value, can't set text.
}
}
-
Re: GUI Calculator: Adding Answer Button? Please Help
Quote:
the answer variable is bound to the if statements.
This doesn't make any sense to me. The variable answer is a method level variable.
I have no idea what you mean by "is bound to". Can you explain?
Your simple example seems to have another variable: answer that is an object. See:
if(answer.isSelected()){
-
Re: GUI Calculator: Adding Answer Button? Please Help
Quote:
Originally Posted by
Norm
This doesn't make any sense to me. The variable answer is a method level variable.
I have no idea what you mean by "is bound to". Can you explain?{
In the if statements, the variable answer is initialized. Outside the if statements, it has no value. This is what I was trying to say by "is bound to".
Quote:
Originally Posted by NORM
Your simple example seems to have another variable: answer that is an object. See:
if(answer.isSelected())
I fixed that just now.. It was supposed to be "answerbutton".