简体   繁体   English

“必须实现继承的抽象方法java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)”是什么意思?

[英]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必须在第五行实现继承的抽象方法java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent) ,但我无法弄清楚它在问什么!

StartRoom implements ActionListener means StartRoom should assumes the contract of ActionListener . StartRoom implements ActionListener意味着StartRoom应该承担ActionListener的约定。 The method actionPerformed( ActionEvent) must be implemented by yourself. 方法actionPerformed( ActionEvent)必须由您自己实现。

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); 例如,如果要委托给另一个类MyActionListener ,则必须在buttonTwo.addActionListener(this);更改用法buttonTwo.addActionListener(this); , replacing this by an instance of MyActionListener . ,取代this通过的一个实例MyActionListener

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

In the later case you mshould remove implements ActionListener from StartRoom class declaration. 在后一种情况下,您应该从StartRoom类声明中删除implements ActionListener

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. 请记住,您只能扩展一个类,但是您可以实现大量的类作为接口。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java:按钮不是抽象的,并且不会覆盖 java.awt.event.ActionListener 中的抽象方法 actionPerformed(java.awt.event.ActionEvent) - java: Button is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener Netbeans无法识别导入java.awt.event.ActionEvent和导入java.awt.event.ActionListener - Netbeans doesn't recognize import java.awt.event.ActionEvent and import java.awt.event.ActionListener jTable中的文本字段比ActionPerformed(java.awt.event.ActionEvent evt)更好! - text field in jTable something better than ActionPerformed(java.awt.event.ActionEvent evt)! 为什么[import java.awt。*]还不包含[import java.awt.event.ActionEvent]? - Why does [import java.awt.*] not also include [import java.awt.event.ActionEvent]? 为什么我总是得到[line:75]错误:必须实现继承的抽象方法java.awt.event.ActionListener? - Why do I keep getting an [line: 75] Error:must implement the inherited abstract method java.awt.event.ActionListener? Java错误:类'Anonymous'必须被声明为abstract或在'ActionListener'中实现抽象方法'actionPerformed(ActionEvent)' - Java Error: Class 'Anonymous' must either be declared abstract or implement abstract method 'actionPerformed(ActionEvent)' in 'ActionListener' Main.GamePanel不是抽象的,并且不会覆盖java.awt.event.KeyListener中的抽象方法keyReleased(java.awt.event.KeyEvent) - Main.GamePanel is not abstract and does not override abstract method keyReleased(java.awt.event.KeyEvent) in java.awt.event.KeyListener 错误:WindowText不是抽象的,并且不会覆盖java.awt.event.MouseListener中的抽象方法mouseExited(java.awt.event.MouseEvent) - Error: WindowText is not abstract and does not override abstract method mouseExited(java.awt.event.MouseEvent) in java.awt.event.MouseListener AListener不是抽象的,并且不会覆盖ActionListener中的抽象方法actionPerformed(ActionEvent) - AListener is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener 类不是抽象的,并且不会重写ActionListener中的抽象方法actionPerformed(ActionEvent) - Class is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM