当前位置: 技术问答>java相关
一个小例子!
来源: 互联网 发布时间:2017-04-12
本文导语: 错误提示!什么意思?编译的时候总出错!! ButtonDemo.java:22: Incompatible type for method. Explicit cast needed to conver t ButtonDemo to java.awt.event.ActionListener. no.addActionListener(this); 源代码 import java.awt.*; import java.aw...
错误提示!什么意思?编译的时候总出错!!
ButtonDemo.java:22: Incompatible type for method. Explicit cast needed to conver
t ButtonDemo to java.awt.event.ActionListener.
no.addActionListener(this);
源代码
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
*/
public class ButtonDemo extends Applet{
String msg="";
Button yes,no,maybe;
public void init(){
yes=new Button("yes");
no=new Button("no");
maybe=new Button("undecided");
add(yes);
add(no);
add(maybe);
yes.addActionListener(this);
no.addActionListener(this);
maybe.addActionListener(this);
}
public void actionPerformed(ActionEvent ae){
String str=ae.getActionCommand();
if (str.equals("yes")){
msg="You pressed yes.";
}
else if (str.equals("no")){
msg="You pressed no.";
}
else {
msg="You pressed undecided.";
}
repaint();
}
public void paint(Graphics g){
g.drawString(msg,6,100);
}
}
ButtonDemo.java:22: Incompatible type for method. Explicit cast needed to conver
t ButtonDemo to java.awt.event.ActionListener.
no.addActionListener(this);
源代码
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
*/
public class ButtonDemo extends Applet{
String msg="";
Button yes,no,maybe;
public void init(){
yes=new Button("yes");
no=new Button("no");
maybe=new Button("undecided");
add(yes);
add(no);
add(maybe);
yes.addActionListener(this);
no.addActionListener(this);
maybe.addActionListener(this);
}
public void actionPerformed(ActionEvent ae){
String str=ae.getActionCommand();
if (str.equals("yes")){
msg="You pressed yes.";
}
else if (str.equals("no")){
msg="You pressed no.";
}
else {
msg="You pressed undecided.";
}
repaint();
}
public void paint(Graphics g){
g.drawString(msg,6,100);
}
}
|
在你使用addActionListener()时,参数务必是implements interface ActionListener的因此:
public class ButtonDemo
extends Applet
implements ActionListene
{
...
}
public class ButtonDemo
extends Applet
implements ActionListene
{
...
}
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。