I agree with @Simon's comment @Simon's comment to use a layout manager, so that you don't need to use hard-coded numbers for positioning with the added benefit of freely resizing your application without worrying about alignment.
As you should have assessed by now, providing a reduced set of mathematical operators and doing calculations are easy, but validating and accepting a simple mathematical expression... not so much. Shameless plug alert Shameless plug alert: my take on a GUI calculator attempts to resolve that by enabling or disabling buttons depending on the existing input, so you may want to consider something like that. In that case, you wouldn't have to be concerned about a user entering + - * / =
.
I agree with @Simon's comment to use a layout manager, so that you don't need to use hard-coded numbers for positioning with the added benefit of freely resizing your application without worrying about alignment.
As you should have assessed by now, providing a reduced set of mathematical operators and doing calculations are easy, but validating and accepting a simple mathematical expression... not so much. Shameless plug alert: my take on a GUI calculator attempts to resolve that by enabling or disabling buttons depending on the existing input, so you may want to consider something like that. In that case, you wouldn't have to be concerned about a user entering + - * / =
.
I agree with @Simon's comment to use a layout manager, so that you don't need to use hard-coded numbers for positioning with the added benefit of freely resizing your application without worrying about alignment.
As you should have assessed by now, providing a reduced set of mathematical operators and doing calculations are easy, but validating and accepting a simple mathematical expression... not so much. Shameless plug alert: my take on a GUI calculator attempts to resolve that by enabling or disabling buttons depending on the existing input, so you may want to consider something like that. In that case, you wouldn't have to be concerned about a user entering + - * / =
.
This is where you run into two usability-breaking problemproblems:
This is where you run into two usability-breaking problem:
This is where you run into two usability-breaking problems:
I think it's fine to get a ScriptEngine
and then eval()
the mathematical expression, but this two-step process is not as instantaneous. Therefore, I will prefer to store engine
as a class-level field, and pressing the =
button will only perform the evaluation. Based on my informal testing, this isYou may want to do a dummy evaluation first so that the very first calculation performed appears faster.
I think it's fine to get a ScriptEngine
and then eval()
the mathematical expression, but this two-step process is not as instantaneous. Therefore, I will prefer to store engine
as a class-level field, and pressing the =
button will only perform the evaluation. Based on my informal testing, this is faster.
I think it's fine to get a ScriptEngine
and then eval()
the mathematical expression, but this two-step process is not as instantaneous. Therefore, I will prefer to store engine
as a class-level field, and pressing the =
button will only perform the evaluation. You may want to do a dummy evaluation first so that the very first calculation performed appears faster.