Layout
As mentioned in @Legato's answer @Legato's answer, you can use a panel and a layout manager to organize the layout, so that you are not constrained to alignment by integers (pixels I believe?).
Validating flow of inputs
You should be able to do something smarter here... how about considering disabling the operators' buttons before the first input, then disable the numbers' buttons before an operator is selected, and then finally enable only the equals button after the second number? Using a Collection
to reference your two sets of buttons will simplify the code to perform this toggling significantly. :)
Storing input values
You shouldn't need a pair of boolean
values on top of your two fields to track whether you have one or two numbers. A single List
will suffice, and you can check if the size()
is 0 or 1.
Implementing the ActionListener
If you happen to be using Java 8, your call to an ActionListener
can be simplified greatly too:
numberButton.addActionListener(event -> {
// assuming input validation is done,
// e.g. the disabling/enabling feature mentioned above
inputs.add(Integer.valueOf(((JButton) event.getSource()).getText()));
// now disable all numeric buttons,
// and enable either the operators or equals buttons
});
Layout
As mentioned in @Legato's answer, you can use a panel and a layout manager to organize the layout, so that you are not constrained to alignment by integers (pixels I believe?).
Validating flow of inputs
You should be able to do something smarter here... how about considering disabling the operators' buttons before the first input, then disable the numbers' buttons before an operator is selected, and then finally enable only the equals button after the second number? Using a Collection
to reference your two sets of buttons will simplify the code to perform this toggling significantly. :)
Storing input values
You shouldn't need a pair of boolean
values on top of your two fields to track whether you have one or two numbers. A single List
will suffice, and you can check if the size()
is 0 or 1.
Implementing the ActionListener
If you happen to be using Java 8, your call to an ActionListener
can be simplified greatly too:
numberButton.addActionListener(event -> {
// assuming input validation is done,
// e.g. the disabling/enabling feature mentioned above
inputs.add(Integer.valueOf(((JButton) event.getSource()).getText()));
// now disable all numeric buttons,
// and enable either the operators or equals buttons
});
Layout
As mentioned in @Legato's answer, you can use a panel and a layout manager to organize the layout, so that you are not constrained to alignment by integers (pixels I believe?).
Validating flow of inputs
You should be able to do something smarter here... how about considering disabling the operators' buttons before the first input, then disable the numbers' buttons before an operator is selected, and then finally enable only the equals button after the second number? Using a Collection
to reference your two sets of buttons will simplify the code to perform this toggling significantly. :)
Storing input values
You shouldn't need a pair of boolean
values on top of your two fields to track whether you have one or two numbers. A single List
will suffice, and you can check if the size()
is 0 or 1.
Implementing the ActionListener
If you happen to be using Java 8, your call to an ActionListener
can be simplified greatly too:
numberButton.addActionListener(event -> {
// assuming input validation is done,
// e.g. the disabling/enabling feature mentioned above
inputs.add(Integer.valueOf(((JButton) event.getSource()).getText()));
// now disable all numeric buttons,
// and enable either the operators or equals buttons
});
Layout
As mentioned in @Legato's answer, you can use a panel and a layout manager to organize the layout, so that you are not constrained to alignment by integers (pixels I believe?).
Validating flow of inputs
You should be able to do something smarter here... how about considering disabling the operators' buttons before the first input, then disable the numbers' buttons before an operator is selected, and then finally enable only the equals button after the second number? Using a Collection
to reference your two sets of buttons will simplify the code to perform this toggling significantly. :)
Storing input values
You shouldn't need a pair of boolean
values on top of your two fields to track whether you have one or two numbers. A single List
will suffice, and you can check if the size()
is 0 or 1.
Implementing the ActionListener
If you happen to be using Java 8, your call to an ActionListener
can be simplified greatly too:
numberButton.addActionListener(event -> {
// assuming input validation is done,
// e.g. the disabling/enabling feature mentioned above
inputs.add(Integer.valueOf(((JButton) event.getSource()).getText()));
// now disable all numeric buttons,
// and enable either the operators or equals buttons
});