简体   繁体   English

如何使用Java中的非抽象类实现ActionListener

[英]How do I implement ActionListener with a non abstract class in Java

Here is a code that i am trying to compile.all i get is error like this 这是我正在尝试编译的代码。我得到的都是这样的错误

s09_02 is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener. s09_02不是抽象的,并且不会覆盖java.awt.event.ActionListener中的抽象方法actionPerformed(java.awt.event.ActionEvent)。

So my question is, how can I implement ActionListener into my class (s09_02) if the class is not abstract? 所以我的问题是,如果类不是抽象的,该如何在我的类(s09_02)中实现ActionListener?

Here is the entire code: (Because I have no idea where the problem could be) 这是完整的代码:(因为我不知道问题可能在哪里)

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...

You need to add public void actionPerformed(ActionEvent e){} in you code as you are implementing ActionListner . 在实现ActionListner在代码中添加public void actionPerformed(ActionEvent e){}

ActionListner is an interface so you need to override the abstract methods of ActionListner ActionListner是一个interface因此您需要覆盖ActionListner的抽象方法

If you are not using any event then remove the implements ActionListner from your code. 如果不使用任何事件,则从代码中删除implements ActionListner

You need to implement the method 您需要实现该方法

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

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

暂无
暂无

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

相关问题 如何使用非抽象类实现ActionListener? Java的 - How can I implement ActionListener with a non abstract class? Java 如何在ActionListener中实现类? - How do I implement a Class into an ActionListener? 如何从 Java 中的非抽象类调用抽象方法? - How do I call an abstract method from a non-abstract class in Java? Java错误:类&#39;Anonymous&#39;必须被声明为abstract或在&#39;ActionListener&#39;中实现抽象方法&#39;actionPerformed(ActionEvent)&#39; - Java Error: Class 'Anonymous' must either be declared abstract or implement abstract method 'actionPerformed(ActionEvent)' in 'ActionListener' 为什么我总是得到[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? 如何使用自定义构造函数实现ActionListener? - How do I implement an ActionListener with custom constructor? 如何实现Java Enumeration Abstract类和接口? - How do you implement a Java Enumeration Abstract class and interface? 如何修复 JAVA VS 代码中的实现错误,“必须实现继承的抽象类” - How do I fix implementation error in JAVA VS code, “must implement the inherited abstract class” 如何在Java中实现抽象的单例类? - How can I implement an abstract singleton class in Java? 如何创建and抽象类的数组,以便可以将其初始化为特定的非抽象类(java) - How can I create an array of and Abstract class such that I can initialize it to a specific non-abstract class (java)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM