gpt4 book ai didi

java - "must implement the inherited abstract method java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)"是什么意思?

转载 作者:行者123 更新时间:2023-11-30 02:40:20 31 4
gpt4 key购买 nike

我的代码遇到了障碍。这是手头的类(class)。

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();
}
}

它说类型 StartRoom 必须实现第五行继承的抽象方法 java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent),但我不知道说出它在问什么!

最佳答案

StartRoom 实现 ActionListener 意味着 StartRoom 应该遵循 ActionListener 的约定。方法 actionPerformed( ActionEvent) 必须由您自己实现。

public class StartRoom extends Room implements ActionListener {
...

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

如果您想委托(delegate)给另一个类,例如 MyActionListener,您必须更改 buttonTwo.addActionListener(this); 中的用法,替换 thisMyActionListener 的实例实现。

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

在后一种情况下,您应该从 StartRoom 类声明中删除 implements ActionListener

关于java - "must implement the inherited abstract method java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41938284/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com