May the force of the Java be in all of us !!!
Shashank Gokhale wrote:Every single book Ive read shows that keylisteners are added to a window or frame only in public static void main().
So I was wondering how do you add a keylistener to a class that does not have the main method, or is this even possible? What I want to do is to have a main method that displays a frame and adds a keylistener to it to tell the frame to close when the user clicks the x or when he presses escape.
The code I have so far for the class (without the main method) is as follows. But how do I add the listener class and will this class be able to draw on the frame displayed by the other class?
luck, db
There are no new questions, but there may be new answers.
May the force of the Java be in all of us !!!
luck, db
There are no new questions, but there may be new answers.
May the force of the Java be in all of us !!!
May the force of the Java be in all of us !!!
Shashank Gokhale wrote:Yes I know that I can use an anonymous inner class to implement a listener, like in the following
Alternatively I can also define a separate class to implement the listener and then use the name of that class as the argument to the addkeylistener statement.
Yet another way is what I did earlier which is to extend the JFrame and implement the keylistener for the same class and then implement the listener methods, but I guess its a bad way to code, so Ill stick with the anonymous inner class.
If I extend a JFrame or some other windowed component into my class, then I wont be able to add a listener to the component I inherited cause there is no object of that component. So if I write
then I wont be able to add a listener to the frame object cause there is none.
If I wanted to add a listener to a frame, then instead of extending the JFrame in my class, I should have typed
luck, db
There are no new questions, but there may be new answers.
May the force of the Java be in all of us !!!
SCJA 6 (Studying for SCJP 6)
Jared Malcolm wrote:Please correct me if I'm wrong (I probably am), but I thought excessive use of the anonymous inner classes could cause a performance hit. Granted I read this when referring to GWT, but would this apply when referring to an application?
SCJA 6 (Studying for SCJP 6)
May the force of the Java be in all of us !!!
To do a great right, do a little wrong - shakepeare. twisted little ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|