• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calculator GUI Clear code

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello. I need help creating a code for a clear button on a calculator GUI. Would I add it as a switch function or do I need to create an action listener function for it? My current code is attached below. Also, how would you change the color of the buttons for the calculator? Would you add it after the add button function? I am very new to making GUI's.

Calc class


Main Class

 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, add a listener to the clear button.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Would I add it as a switch function or do I need to create an action listener function for it?



You should add a unique ActionListener to all the buttons instead of creating an ActionListener with a switch statement.

So, in general, an ActionListener should only perform one function.

It is possible to share a listener with multiple components when the functionality is generic to the button. For example check out:

https://coderanch.com/t/630056/java/Adding-ActionListener-JButtons#2884374
 
Pauleen Mona
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Thank you! That's just how our lab professor told us to write the code.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pauleen Mona wrote:. . . Thank you!

That's a pleasure

That's just how our lab professor told us to write the code.

Obviously somebody who knows what they are doing

As I said, you will have to do some arithmetical jiggery‑pokery to get the numbered buttons into their correct positions. If you used grid bag
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A nice java 8 way to avoid the switch statement is to pass the functionality of an operator button to the code that you have now in the anonymous actionListener.

For instance, introduce four new fields to your class:

BinaryOperator<Double> add = (a, b) -> a +b;
BinaryOperator<Double> times = (a, b) -> a * b;
...
BinaryOperator<Double> operator;

In the action listener of the 'add'  button, do: operator = add;

and finally, in the equals action listener, you get your result with: operator.apply(left operand, right operand);

And believe me, it is easier done than said!
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yesterday, I wrote:. . . arithmetical jiggery‑pokery to get the numbered buttons into their correct positions. If you used grid bag

. . . 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.
789
456
123
.0=
+-C
*/AC
0, 01, 02, 0
0, 11, 12, 1
0, 21, 22, 2
0, 31, 32, 3
0, 41, 42, 4
0, 51, 52, 5
The second table shows the x, y positions, or column‑row. Note 0, 0 is top left. Note the 0 button is in column 3. The other buttons' columns and all the rows can be calculated simply with combinations of the + − divide (/) and remainder (%) operators.
If you use gridbag, you will want all the buttons with the same size, separation and behaviour when you resize the display. In which case I suggest you want the constraints with the same width/height throughout (1 each way), the same weights (maybe 1.0 both ways), the same fill (try both ways), anchor (try centre), internal padding (try 10 both ways) and insets (try new Insets(5, 5, 5, 5)). Those Insets would reduce the button size to 40×35px. Then you only need to change the two position fields from button to button. You might have to alter the widths if you need a row with other than three buttons on.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic