简体   繁体   中英

what does “must implement the inherited abstract method java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)” mean?

I hit a roadblock in my code. this is the class in hand.

public class StartRoom extends Room implements ActionListener {

   JButton buttonTwo;

   public StartRoom() {
      start();
      buttonOne = new JButton("Go to door.");
      buttonTwo = new JButton("Look at skeleton.");
      label = new JLabel("You walk into the dungeon, the room is covered with vines. There is a skeleton sitting near the northern door. What do you do?");
      panelOne.add(label);
      panelOne.add(buttonOne);
      buttonOne.addActionListener(this); 
      buttonTwo.addActionListener(this);
   }

   class MyActionListener implements ActionListener {
      @Override
      public void actionPerformed(java.awt.event.ActionEvent ae) {

      } 
   }

   public static void main( String[]args ) {
      new StartRoom();
   }
}

It says that The type StartRoom must implement the inherited abstract method java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent) on line five, but I can't figure it out what it's asking!

StartRoom implements ActionListener means StartRoom should assumes the contract of ActionListener . The method actionPerformed( ActionEvent) must be implemented by yourself.

public class StartRoom extends Room implements ActionListener {
   ...

   @Override
   public void actionPerformed(java.awt.event.ActionEvent ae) {
      // your code here....
   } 
}

If you want to delegate to another class, MyActionListener , for example, you have to change the usage in buttonTwo.addActionListener(this); , replacing this by an instance of MyActionListener .

MyActionListener toto = new MyActionListener();
buttonTwo.addActionListener( toto );

In the later case you mshould remove implements ActionListener from StartRoom class declaration.

When you implement something you are using that class as an interface, which means you must use and redefine every method in the class you are implementing. However, if you want to use that class's method(s) as-is then you may extend it and make that class a superclass of your class. Keep in mind that you may only extend one class, but you can implement a large number of classes as interfaces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM