Computer Science Canada Keyboard question |
Author: | Flikerator [ Thu Aug 09, 2007 7:59 pm ] | ||||
Post subject: | Keyboard question | ||||
I just started learning Java (I'm not asking for a lecture on why I shouldn't use Java either), and I've been stuck on getting input from the keyboard to work for a few hours. Its probably not that difficult, but I can't get it to work at all. I've looked up a ton of examples. They each do it differently as well. In terms of Turing I want something that works relative to this;
And what I have in Java (The latest most cutdown version);
Don't worry too much about the imports. I know some of them aren't being used right now. The reason I have java.awt.* is because if I don't, then it claims it can't find the 'Event' methods, even when I had java.awp.Event.*. I'm still learning how Java works, and I have the basic idea but not completely, obviously. Having the program continue without loops is still bugging me (The Woes of Turing). With this good I'm basically grasping for straws. Experimenting until I find something that works, gain some understanding on how it all comes together as I go along. Any assistance? |
Author: | Flikerator [ Fri Aug 10, 2007 2:11 am ] | ||
Post subject: | Re: Keyboard question | ||
I feel I'm so close, but I keep getting this error: events.Test is not abstract and does not override abstract method keyReleased(java.awt.event.KeyEvent) in java.awt.event.KeyListener
|
Author: | Aziz [ Fri Aug 10, 2007 9:19 am ] | ||||||||||||||||||||||
Post subject: | RE:Keyboard question | ||||||||||||||||||||||
There are a few things: 1) Each class should be in a separate file, ClassName.java 2) test class should not extend JFrame. It's not actually a JFrame, is it? All it does is create an instance of mainFrame(). 3) Classes should be named in FirstLetterCase, notLikeThis. 4) test should not implement KeyListener. Let me explain about EventListeners all Swing components can add certain EventListeners. When something happens on that component (such as a key being typed, or a button being pressed), the internal mechanisms "fire" an Event. If that component has an appropriate listener added to it, it will call a specific method of that selected listener. All listeners inherit from EventListener, and each (almost) Listener has an associated Event. For example, A button being clicked. When you make a button, you do like so:
When you click that button, it's internal code takes care of certain things, including drawing the clicking animation and "firing" and event. In this case, the button's click would fire an ActionEvent. But our code doesn't add an ActionListener to the button. Let's do that. An ActionListener is another class. Or rather, it is an interface. It has a single method:
We can't instantiate an object of an interface. We have to create a class that implements ActionListener. We can have our main class do this:
Now, in the constructor, when we set up components, we will make a JButton, and use the JButton's addActionListener(ActionListener) method to add the current MainClass object ("this") as an ActionListener (because MainClass implements ActionListener, it "is an" ActionListener, and you could pass it to any method that calls for an ActionListener). When an ActionEvent is fire by a JButton (like clicking it), it will call the associated ActionListener's actionPerformed() method, and pass an ActionEvent object (which can tell you certain information, such as the source of the event). Watch:
Test it out, when the button is clicked, actionPerformed() in MainClass is run. Of course, you need to import JFrame, ActionEvent, ActionListener, and JButton. However, this is often hackish, especially if you have many buttons, etc. You'll have to use the "getSource" or "getActionText" to figure out which button fired the event, and act on there. That's how the Swing Tutorial does it, and I don't like it (neither do others around here...) (I'm writing this all off the top of my head, so forgive me if I get any method names wrong or wrong) What we should do is create a separate ActionListener object for each button. Which would mean we'd have to create a separate class. But then, it'd be a lot of work to make those classes access the features of MainFrame, and we don't want that. Hold on. We can use Java's inner classes:
Then we can make an object of each:
When we click button1, the button fire an ActionEvent, recognizes that it has an associated ActionListener, and calls that ActionListener's actionPerformed. This ActionListener happens to be b1AListener, an object of Button1ActionListener. So the actionPerformed() method of Button1ActionListener gets called. That's a lot of clutter at class-level. We don't need those class outside of the place where we create them. Of course, we can strip the "private" modifier of the classes, and declare them inside the methods, but it's not much better. We'll never need the classes more than once, will we? Java offers another structure to make our life easier: anonymous inner classes. We don't even have to give the class a name! We'll go back to our one-button example. Watch:
(See bottom of post for links). In this way of doing this, we've defined the class as soon as we've instantiated it. When you compile, you'll see a file called "MainClass$1.class". This is the class definition for that class. A note, these classes can only affect local variables that are declared "final". The best way to deal with events that do something is to call a method to handle the event, such as button1ActionPerformed. We can make it better. We only need to use "listener" once. We don't need to store it in a variable. We can do it write in the argument list for addActionListener:
The same can be said for KeyListeners. You'll have to add a KeyListener to MainClass. So:
But we only need the one method, probably keyPressed. However, we have to define all 3 methods, because KeyListener is an interface, and by default all methods are abstract: they have no implementation, and thus calling them (which might happen, the compiler doesn't know) would not work. Luckily, we can use the KeyAdapter class. It is simply a class that implements KeyListener (so it is-a KeyListener and can go where a KeyListener can). All it's methods are defined with empty implementation (there's no code in the method body, but it does exist). So we can override only the methods we want: Anonymous classes can extend classes as well, not just interfaces. So we can do:
Here's a complete working example: MainFrame.java
Refer to these links for for help Swing Tutorial - http://java.sun.com/docs/books/tutorial/uiswing/index.html Writing Event Listeners - http://java.sun.com/docs/books/tutorial/uiswing/events/index.html Swing Basics tutorial by rizzix - http://compsci.ca/v3/viewtopic.php?t=1975 Use Swing, for crying outloud, tip by wtd http://compsci.ca/v3/viewtopic.php?t=14235 Now, I just got paid for writing all that (Help desk jobs can be pretty liesurely), and I've got better things to do (like read Wookiepedia!), so I suggest you read all the tuts and what not, and experiment. And just post if you have more issues. Don't forget the Jave API Documentation is the an amazing resource... http://java.sun.com/javase/6/docs/api/ |
Author: | Flikerator [ Wed Aug 15, 2007 2:30 am ] |
Post subject: | Re: Keyboard question |
Thanks for all the help, and especially the links. I've been reading through the tutorials. It makes a lot more sense actually reading how Java works instead of downloading an IDE and just messing around. Who could have guessed =) Thanks again. |
Author: | Nick [ Wed Aug 15, 2007 5:06 am ] |
Post subject: | Re: RE:Keyboard question |
Aziz @ Fri Aug 10, 2007 9:19 am wrote: Now, I just got paid for writing all that (Help desk jobs can be pretty liesurely)
dude could ya hook me up with a turing help desk job... i have absoultly nothing to do with my time and although im no expert in turing i still know A LOT!... well mroe than the average programmer i read some problems ppl have in turing and although most are already answered (becuase im new to this site) i knew excatly the problem and how to fix... so could you get back to me on this... please? |
Author: | Aziz [ Wed Aug 15, 2007 7:52 am ] |
Post subject: | RE:Keyboard question |
Hehe. I don't work for a Turing helpdesk (he was asking a Java question, anyways ![]() |
Author: | Nick [ Wed Aug 15, 2007 8:17 am ] |
Post subject: | RE:Keyboard question |
lol awwww i thought u worked for compsci and they paid u to help ppl ![]() ![]() |
Author: | Aziz [ Wed Aug 15, 2007 8:26 am ] |
Post subject: | RE:Keyboard question |
Heh . . . Dan wouldn't pay anyone to answer questions on this site. There's enough people willing to do it for free (wtd, rizzix...). Though, they do get to expel radiation in the form of [corrupted] moral wisdom while doing so. |
Author: | Nick [ Wed Aug 15, 2007 8:27 am ] |
Post subject: | RE:Keyboard question |
lmao well in a way u do get paid... bits and enough bits and i can get that web hosting, c'mon c'mon c'mon!!! |
Author: | Aziz [ Wed Aug 15, 2007 8:31 am ] |
Post subject: | RE:Keyboard question |
I wish. It'd probably more cost-effective to just buy hosting. It's not that expensive, is it? I've been here for nearly three years... and I've only got 720 bits! But, I would visit in laps ![]() |
Author: | Nick [ Wed Aug 15, 2007 8:34 am ] |
Post subject: | RE:Keyboard question |
lmao yea and for contests most top prizes are only around 200 bits like how anyone ever supposed to save up with that? :S |