For practice, I am trying to create a fully functional calculator. I copied the ActionListener class of my code below. I feel like my this method for getting the users input, storing it as an integer/double does not work well as it is too basic and does not work in all cases. I was wondering if anyone can help me figure out a way to structure my code, get user input for two numbers and a symbol, and basically create a fully function GUI calculator. My problem is getting user input from my JButton, storing as a number that in way like a calculator does, (press 1 twice == 11) and using it for later calculation.
I appreciate any advice in this regard.
private class TheHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
while(equals!=event.getSource())
if(one==event.getSource()){
result.setText("1");
num1=1;
}
else if(two==event.getSource()){
result.setText("2");
}
else if(three==event.getSource()){
result.setText("3");
}
else if(four==event.getSource()){
result.setText("4");
}
else if(five==event.getSource()){
result.setText("5");
}
else if(six==event.getSource()){
result.setText("6");
}
else if(seven==event.getSource()){
result.setText("7");
}
else if(eight==event.getSource()){
result.setText("8");
}
else if(nine==event.getSource()){
result.setText("9");
}
else if(zero==event.getSource()){
result.setText("0");
}
}
}
You should remove the while at all.
I would suggest to use numeric vars like
Each event should just set digit