Why can't be use a method of a predefined interface without implementing that interface in the class that use this method?
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Daniel_Laslau
Posted On:   Thursday, April 12, 2012 07:23 AM

package testcardlayout2; import java.awt.*; import java.awt.event.*; public class TestCardLayout2 extends Frame implements ActionListener{ Panel tab; public TestCardLayout2 () { super (" Test CardLayout "); Button card1 = new Button (" Card 1"); Button card2 = new Button (" Card 2"); Panel buttons = new Panel (); buttons . add( card1 ); buttons . add( card2 ); tab = new Panel (); tab . setLayout ( new CardLayout ()); TextField tf = new TextField (" Text Field "); Button btn = new Button (" Button "); tab .add (" Card 1", tf); tab .add (" Card 2&quo   More>>
			
package testcardlayout2;
import java.awt.*;
import java.awt.event.*;


public class TestCardLayout2 extends Frame implements ActionListener{

Panel tab;
public TestCardLayout2 () {
super (" Test CardLayout ");
Button card1 = new Button (" Card 1");
Button card2 = new Button (" Card 2");
Panel buttons = new Panel ();
buttons . add( card1 );
buttons . add( card2 );
tab = new Panel ();
tab . setLayout ( new CardLayout ());
TextField tf = new TextField (" Text Field ");
Button btn = new Button (" Button ");
tab .add (" Card 1", tf);
tab .add (" Card 2", btn);
add ( buttons , BorderLayout . NORTH );
add (tab , BorderLayout . CENTER );
pack ();
show ();
card1 . addActionListener ( this );
card2 . addActionListener ( this );
}
public void actionPerformed ( ActionEvent e) {
CardLayout manager= ( CardLayout ) tab. getLayout ();
manager.show (tab , e. getActionCommand ());
}

public static void main(String[] args) {
TestCardLayout2 f = new TestCardLayout2 ();
f.show ();
}
}


Why above code doesn't work if TestCardLayout2 class doesn't implement ActionListener interface?

I don't understand this because I know that an interface is a group of related methods with empty bodies(which does nothing in an algorithm like the one that handle user actions) and I have in TestCardLayout2 class all the methods to listen an action.

Why is it also necessary to implement ActionListener interface? How actually works an interface?

   <<Less
About | Sitemap | Contact