K. Tsang CEng MBCS PMP PMI-ACP OCMJEA OCPJP
YesJanis Mittelstaedt wrote: . . .
1. Was I right in making all those inner classes for the buttons etc.?
That is an abomination against object‑oriented programming.However, after looking at code in the internet I've noticed that most of the time the class itself simply implements ActionListener . . .
There is no right way to make a second window. You use a dialogue.
2. As you may or may not have notice, when clicking on the credits menuItem a new Frame will be created. I didn't know how else I could open another window. . . . What's the right way to make such a window?
You should never try to squeeze everything into one class. You should start with a class which calculates ℉↔℃, which can be a utility class: private constructor, and static fToC and cToF methods only. You should have that class running correctly before you write a single byte of GUI code.
3. Is the design of my program right? . . . (Should I have created more than one class? . . .
You asked for it
4. Finally, is there anything which I could've done better? Is any code maybe badly written or doesn't follow the coding convention etc.? . . .
We usually discuss GUI design in our GUI forum, but rather than moving this discussion I can duplicate it in the other forum, where Rob is in charge. He is much benign‑er than me!PS.: I hope this is in the right section. . . .
K. Tsang wrote:As for action listeners (Q1), I personally haven't used event.getSource() but it really again depends on what you want to do with the action. If the action is inter-related or should I say tightly-coupled with some other component, then inner class would be a good choice. Else it is practical to separate it into its own class.
K. Tsang wrote:Also you may also want to look into AbstractAction. The programming is the same (override actionedPerformed method) only the class declaration is different. Instead of implements ActionListener, it is extends AbstractAction.
K. Tsang wrote:For your credit action (Q2), use a JDialog instead of JFrame. Even this JFrame is another variable independent from the main JFrame.
Campbell Ritchie wrote:You should never try to squeeze everything into one class. You should start with a class which calculates ℉↔℃, which can be a utility class: private constructor, and static fToC and cToF methods only. You should have that class running correctly before you write a single byte of GUI code.
Campbell Ritchie wrote:
1: Recommend not to use on demand imports. Use the type of import without * 2: Why did you choose those two initial values for your ℃ and ℉ fields? 3: What errors do you expect from using integer arithmetic for your conversion? 4: You are using a boolean to decide conversion direction. Are you not aware of enumerated types, which allow you to write C_TO_F or similar? 5: Instead of the cToF flag, you might do better to have two methods, one called from the CtoF button and the other from the FtoC button. 6: Try these Unicode escapes: \u2103 \u2109
Campbell Ritchie wrote:That is an abomination against object‑oriented programming.
However, after looking at code in the internet I've noticed that most of the time the class itself simply implements ActionListener . . .
That is an abomination against object‑oriented programming.
Yes. It is probably better to be able to see which classes have been imported than to try guessing with *.Janis Mittelstaedt wrote: . . . 1: So you mean importing 'javax.swing.JFrame' etc., right? . . .
So you can accurately convert 21℃ to ℉ and convert it back? Why won’t it work for doubles?3: I'm sorry, english isn't my main language so I'm having trouble understanding what exactly you mean. . . . The convert button won't work if you put in a double or String and otherwise the conversion seems to work fine.
Too much time spent hanging about the Unicode website.. . .
6: That's a good idea . . .
You are doing quite well there. . . challenge for me to see if I could do it without any tutorial (or IDE)
People who write that sort of thing on the internet ought to have their fingernails pulled out very slowlyCampbell Ritchie wrote:That is an abomination against object‑oriented programming.
That's good to know!
There is nothing wrong with a Listener which implements several interfaces.Rob Camick wrote: . . . For example if you want to create a class that does mouse handling for you you might do:
I've noticed that most of the time the class itself simply implements ActionListener . . .
Campbell Ritchie wrote:So you can accurately convert 21℃ to ℉ and convert it back? Why won’t it work for doubles?
Campbell Ritchie wrote:And there is something else. Try this:-
No. I don’t like initialisers, but that is simply a personal preference. That code was not a block, but a complete class. Compile it and see what happens.Janis Mittelstaedt wrote: . . .
Do you mean the non-static block? ...
I think that would work, too, but am not certain.Janis Mittelstaedt wrote: . . . would you import the one from util and use 'javax.swing.Timer' for the other (or the other way around)? . . .
He does not suffer fools gladly. But this tiny ad does:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|