[英]How can I implement ActionListener with a non abstract class? Java
[英]How do I implement ActionListener with a non abstract class in Java
這是我正在嘗試編譯的代碼。我得到的都是這樣的錯誤
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...
在實現ActionListner
在代碼中添加public void actionPerformed(ActionEvent e){}
。
ActionListner
是一個interface
因此您需要覆蓋ActionListner
的抽象方法
如果不使用任何事件,則從代碼中刪除implements ActionListner
。
您需要實現該方法
@override
public void actionPerformed(ActionEvent e){
//code that dose something
}
http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.