【问题标题】:How do I implement ActionListener with a non abstract class in Java如何在 Java 中使用非抽象类实现 ActionListener
【发布时间】:2014-05-14 16:33:34
【问题描述】:

这是我正在尝试编译的代码。我得到的只是这样的错误

s09_02 不是抽象的,不会覆盖抽象方法 java.awt.event.ActionListener 中的 actionPerformed(java.awt.event.ActionEvent)。

所以我的问题是,如果类不是抽象类(s09_02),我该如何在我的类中实现 ActionListener?

这是完整的代码:(因为我不知道问题出在哪里)

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class s09_02 extends Applet implements ActionListener{
public void init()
 {
  setLayout(null);
  setBackground(new Color(0,10,100));
 }
 public void paint(Graphics p){String t=null;
 int x,y,w,h,r,g,b;
 t=getParameter("xx");
 x=Integer.parseInt(t);
 t=getParameter("yy");
 y=Integer.parseInt(t);
 t=getParameter("ww");
 w=Integer.parseInt(t);
 t=getParameter("hh");
 h=Integer.parseInt(t);
 t=getParameter("rr");
 r=Integer.parseInt(t);
 t=getParameter("gg");
 g=Integer.parseInt(t);
 t=getParameter("bb");
 b=Integer.parseInt(t);
 p.setColor(new Color(r,g,b));
 p.fillRect(x,y,w,h);

} }

<html>
<body>
<applet code="s09_02.class" width=400 height=400>
<param name="xx" value="25"><param name="yy" value="25">
<param name="ww" value="150"><param name="hh" value="150">
<param name="rr" value="0"><param name="gg" value="150">
<param name="bb" value="100">
</applet>
</body>
</html>


Also suggest me what changes should i make in this code so that code runs properly ??
Thanks...

【问题讨论】:

  • 按照编译器告诉你的去做:实现方法public void actionPerformed(ActionEvent ae){}
  • 1) 为什么要编写小程序?如果是由于规范。老师请发给Why CS teachers should stop teaching Java applets。 2) 为什么选择 AWT 而不是 Swing?请参阅我在 Swing extras over AWT 上的回答,有很多放弃使用 AWT 组件的充分理由。

标签: java applet awt actionlistener actionevent


【解决方案1】:

在实现ActionListner 时,您需要在代码中添加public void actionPerformed(ActionEvent e){}

ActionListner 是一个interface,所以你需要重写ActionListner 的抽象方法

如果您不使用任何事件,请从您的代码中删除 implements ActionListner

【讨论】:

  • :谢谢您的建议。通过从我的代码中删除 Action 侦听器,它对我有用...
  • @user3046211 如果有帮助,请接受这个答案。
【解决方案2】:

你需要实现方法

    @override
public void actionPerformed(ActionEvent e){
//code that dose something
}

http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html

【讨论】:

    猜你喜欢
    • 2013-03-11
    • 2020-10-20
    • 1970-01-01
    • 2011-01-28
    • 2016-08-16
    • 2017-02-27
    • 2011-12-01
    • 2013-01-30
    • 1970-01-01
    相关资源
    最近更新 更多