Skip to main content
gamedev.net
🔒 Locked

[java] question about the ActionListener

Started by jake_Ghost Feb 17, 2005 at 9:09 AM 3 replies 1.1k views
Original Post
jake_Ghost
jake_Ghost
in class today i was trying to use the action listener which wasnt working properly until i added the jave.awt.event.* and the java.awt.*;. I would have thought that just importing the java.awt would make it run but it didnt. Could someone tell me how it works to add to my understanding of java? hers wat i had done... // The "EventTest" class. import java.awt.event.*; import java.awt.*; import java.applet.*; import java.lang.*; public class EventTest extends Applet implements ActionListener { // Place instance variables here Label lblDisplay = new Label (" "); TextField txtInput = new TextField (" "); Button btnDisplay = new Button ("Display"); public void init () { // Place the body of the initialization method here add (lblDisplay); add (txtInput); add (btnDisplay); btnDisplay.addActionListener (this); } // init method public void actionPerformed (ActionEvent event) { lblDisplay.setText (txtInput.getText ()); } } // EventTest class
CaptainJester
CaptainJester
All those import statements:

import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.lang.*;


import packages for use in your source file. Packages contain all the classes that you can use. ActionListener is in the java.awt.event package. You need the java.awt package for Button, TextField etc...
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
tebriel
tebriel
To find out what is in which package, search the API documentation. It'll say java.awt.event, etc. on the top. Do that for every type of class you're using, and import those... you should be looking at the API docs alot to see how stuff is used anyway. :)

Either that, or get Eclipse, which can "Organize Imports" automagically for you! =)
jake_Ghost
jake_Ghost
alrite thx for clearing that up
Lyrrwen
Lyrrwen
Just to try to make it a little more clear, when you import a package, it only imports those classes that are directly in the package. It doesn't import 'sub-packages' recursively. This seemed a little unintuitive to me too, at first.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.