Would I add it as a switch function or do I need to create an action listener function for it?
Campbell Ritchie wrote:Beware of calling things btn_zero; avoid the _ character and don't start the identifier with its type.
You don't want to write out all those Listeners individually. I think you want a loop to create all the number buttons, and add listeners in the same loop. Don't write the numbers more than once. Once you have put the number onto a button, use getText() or similar to get the number back. You can of course add a listener which changes the colour of the button, but why? You can call setBackground() or setForeground() on any Component, which includes buttons.Now you will have to change all that lot because you have now got all the buttons in the wrong order on your display. I shall let you work out the arithmetic for the loop. You may find it easier to instantiate all those buttons in order if you create a private method returning a JButton.
Go through the Java® Language Specification. I won't tell you what you will find in that section.
Most people have stopped writing anonymous classes for functional interfaces like action listener. Use a λ expression instead. That is what line 5 contains.
That's a pleasurePauleen Mona wrote:. . . Thank you!
Obviously somebody who knows what they are doingThat's just how our lab professor told us to write the code.
There are three kinds of actuaries: those who can count, and those who can't.
. . . and I appear not to have finished the sentence. Find out about gridbag. This tutorial should only be watched with a large cup of coffee. Cay Horstmann has a way of making gridbag easier to use. I think you can do some arithmetical jiggery‑pokery to get the coordinates right. If you have 0-9+-*/.= buttons, as well as C and AC, you have 18. Let's make your button panel's preferred size 150×270 which is enough space for three columns and six rows of 50×45px buttons. Use AC for all clear: delete the entire text entered. That makes a button count dividing exactly by three.Yesterday, I wrote:. . . arithmetical jiggery‑pokery to get the numbered buttons into their correct positions. If you used grid bag
7 | 8 | 9 |
4 | 5 | 6 |
1 | 2 | 3 |
. | 0 | = |
+ | - | C |
* | / | AC |
0, 0 | 1, 0 | 2, 0 |
0, 1 | 1, 1 | 2, 1 |
0, 2 | 1, 2 | 2, 2 |
0, 3 | 1, 3 | 2, 3 |
0, 4 | 1, 4 | 2, 4 |
0, 5 | 1, 5 | 2, 5 |
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |