Im a trying to do a basic calculator. At first i only had two buttons " " and "-", when i pressed one of the two buttons the text is displayed. For the next step, i want to display the result only when the "=" button is pressed. So basically " " and "-" buttons are used to calculate and "=" to display the calcul. But nothing is displayed when "=" is pressed. Thanks for your help.
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main extends JFrame implements ActionListener{
JTextField tf1,tf2,tf3; JButton b1,b2, b3;
Main() {
tf1 = new JTextField();
tf1.setBounds(50,50,150,20);
tf2 = new JTextField();
tf2.setBounds(50,100,150,20);
tf3 = new JTextField();
tf3.setBounds(50,150,150,20);
tf3.setEditable(false);
b1 = new JButton(" ");
b1.setBounds(50,200,50,50);
b2 = new JButton("-");
b2.setBounds(120,200,50,50);
b3 = new JButton("=");
b3.setBounds(190,200,50,50);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
add(tf1);add(tf2);add(tf3);add(b1);add(b2);add(b3);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public static void main(String[] args){
new Main();
}
@Override
public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(tf1.getText());
int b = Integer.parseInt(tf2.getText());
int c = 0;
Object buttonPressed = e.getSource();
if (buttonPressed == b1 ){
c= a b;
if(buttonPressed == b3){
tf3.setText(String.valueOf(c));
}
}
if(buttonPressed == b2){
c=a-b;
if(buttonPressed == b3){
tf3.setText(String.valueOf(c));
}
}
}
}
CodePudding user response:
The problem is that you nested your if statements. Look here:
if (buttonPressed == b1 ){
c= a b;
if(buttonPressed == b3){
tf3.setText(String.valueOf(c));
}
}
This only executes when the variable buttonPressed is equal to b1. But at the same time buttonPressed will never be b3, so that part never gets to execute.
You will need a sequence of if statements like so:
if (buttonPressed == b1) { ... }
if (buttonPressed == b2) { ... }
if (buttonPressed == b3) { ... }
You could also use different ActionListeners for each button.
CodePudding user response:
The problem is that button
can't be both b1
and b3
. You could introduce another variable that keeps track of the operator the user click on. It could be a character, a string, or a button. When the user click the "=" button, you then check the operator and perform the appropriate operation and display the result.
Another thing you have to think about is: what does the program do when the user enter two numbers and click "=" button without providing an operator?
Here's my proposed solution based on your code. Button b1
and b2
only update the variable operator
. Button b3
performs the computation.
public class Main extends JFrame implements ActionListener{
JTextField tf1,tf2,tf3; JButton b1,b2, b3;
char operator = '?';
Main() {
...
b1.addActionListener(e->operator=' ');
b2.addActionListener(e->operator='-');
b3.addActionListener(this);
...
}
@Override
public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(tf1.getText());
int b = Integer.parseInt(tf2.getText());
if (operator == ' ') tf3.setText("" (a b));
if (operator == '-') tf3.setText("" (a-b));
if (operator == '?') tf3.setText("Choose operator");
}
}