Hi guys,
I wrote the small application below while learning events.
The application now works, however, for some reason I get compilation errors if I do not include the extra "import java.awt.event.*;" at the top. This seems strange because I already have import java.awt.*; which is more general.
Can anybody explain why the specific one with the extra ".event.*;" is required???
Cheers
Mario
---------------------------------------------
import java.util.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*; //this one is also needed!!!
public class EventTest extends Applet{
//2ndtechnique//public class EventTest extends
Applet implements ActionListener{
Button monbouton1;
public void init() {
setLayout(new FlowLayout());
monbouton1 = new Button("Action");
MyActionListener listenerVar = new MyActionListener();
monbouton1.addActionListener(listenerVar); //assigning listener to the button
//2ndtechnique//monbouton1.addActionListener(this);
this.add(monbouton1); //using "this" is not required but shown anyways
}
//2ndtechnique//public void actionPerformed(ActionEvent e) {
//2ndtechnique// if (e.getSource() == monbouton1) {
//2ndtechnique// System.out.println("Statement action in the listener");
//2ndtechnique// }
//2ndtechnique//}
}
//This is the listener
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent actione){
System.out.println("Statement action in the listener"); //This is where the action is!!!
}
}
<html>
<head><title>EventTest (EventTest.htm)</title></head>
<body>
Here is the EventTest:
<br clear="all">
<APPLET CODE="EventTest.class" WIDTH=450 HEIGHT=150>
</APPLET>
</body>
</html>