• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calculator gui

 
Krystian Kowalski
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. I have made a calculator with gui (in 1 st version i was using awt). All algoritms to count are in another class, gui is in another class. That's what i have (abbreviation version ;p i think i don't have to past all my code ):



Bassicly everything we click our action listener past it to textfield and class Licz is converting from string to int and counting it and everything is fine, but i wanted to make nice calculator with images etc;p And here is my idea(in swing!):



And here is my question- is there any other way to make it easier? First- I have to load all images to all buttons separately, and it's a lot of code( i have like 17 buttons) and do i have to use single actionListener for single key and setting TextField ? Is there any other possibility to don't use a single ActionListeners for single keys? If i can make it easier and better please help

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can certainly streamline some of it. The most obvious are the number buttons. They all behave in exactly the same way, except the number is different. So you can do the following:

- Instead of using b6, b7 etc, put the references in an array.
- Loop round the array to load images (the filename only changes as far as the number, I assume?)
- Parameterize the ActionListener so you can add it within the loop, changing the behaviour just by passing the number into a constructor.

For the last bit, I mean something like this:



You may be able to something similar with some of the other buttons. One thing I'd avoid, though, is having ActionListeners that have to check which button was clicked to decide what to do. You're usually much better having separate ActionListeners (as you have already) than going that route. It's only when the behaviour is sufficiently similar that it's worth reusing a class (and even then I'd use different instances).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!