. . . and also go through some style guidelines. I don't agree with everything there, nor in the Sun style guide referenced there, but things like consistent indentation and use of whitespace would make your code much easier to read.
So many people are taught to use \n, but here you find you ought to use %n rather than \n. You should use printf rather than println to get %n to work.
So many people are taught to use addActionListener(this), but that is by no means object-oriented programming. You should create classes which implement the ActionListener interface, rather than have a display Component implement that interface. Even though many books, and the Java tutorials use that comstruct, I believe you should never use it.
It is worth checking what sort of Listener can be added to a text component. You can find Bruce Eckel's Thinking in Java which has an example program in which displays all the addXXXListener methods a particular class has. Or you can go to the API documentation and count them by hand. there may be scores of them. Remember to look in the blocks headed "methods inherited from XYZ". You will find an ActionListener is likely to be fired whenever you use the enter key inside your text field. You can find other Listeners which are fired whenever you alter add or remove text from it. Consider which is most appropriate to your requirements.
Joined: May 31, 2011
Campbell Ritchie wrote:So many people are taught to use addActionListener(this), but that is by no means object-oriented programming.
Thanks for the information...
Joined: Oct 13, 2005
. . . at least I don't think it's object-oriented. You end up with a block of if-elses. The worst I ever saw was from somebody on the Ranch a few years ago, who was trying to make a calculator, and ended up with an actionPerformed method with this sort of if blocks:. . . or something like that. A whole screenful. And you could have reduced it to a simple for loop with the right class.She seemed to be having so many problems with the rest of her code, I didn't have the heart to tell her to chuck ¾ of that class in the recycle bin.