
java程序运行错误
packagetextPackage;importjava.awt.Button;importjava.awt.Dialog;importjava.awt.FlowLay...
package textPackage;
import java.awt.Button;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.JobAttributes.DialogType;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class AppletTest3 extends Frame implements ActionListener
{
static Frame frm = new Frame();
static Label welcome = new Label("请关闭对话框");
static Dialog dlg = new Dialog(frm);
static Button Close_btn = new Button("关闭");
static Button Cancel_btn = new Button("取消");
static WinLis wlis = new WinLis();
public static void main (String args[])
{
frm.setTitle("Dialog");
frm.setSize(200,150);
frm.setLocation(200, 200);
welcome.setAlignment(Label.CENTER);
dlg.setTitle("你确定要关闭?");
dlg.setSize(160, 120);
dlg.setLayout(new FlowLayout(FlowLayout.CENTER,5,30));
dlg.add(Close_btn);
dlg.add(Cancel_btn);
Close_btn.addActionListener((ActionListener) frm);
Cancel_btn.addActionListener((ActionListener) frm);
frm.addWindowListener(wlis);
frm.setVisible(true);
}
static class WinLis extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
dlg.setLocation(230, 200);
dlg.show();
}
}
public void actionPerformed(ActionEvent e)
{
Button btn = (Button)e.getSource();
if(btn == Close_btn)
{
dlg.dispose();
frm.dispose();
}
else if(btn == Cancel_btn);
dlg.hide();
}
}
编译没问题,运行出现如下问题:
Exception in thread "main" java.lang.ClassCastException: java.awt.Dialog cannot be cast to java.awt.event.ActionListener
at textPackage.AppletTest3.main(AppletTest3.java:38)
38,39行为:
Close_btn.addActionListener((ActionListener) frm);
Cancel_btn.addActionListener((ActionListener) frm); 展开
import java.awt.Button;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.JobAttributes.DialogType;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class AppletTest3 extends Frame implements ActionListener
{
static Frame frm = new Frame();
static Label welcome = new Label("请关闭对话框");
static Dialog dlg = new Dialog(frm);
static Button Close_btn = new Button("关闭");
static Button Cancel_btn = new Button("取消");
static WinLis wlis = new WinLis();
public static void main (String args[])
{
frm.setTitle("Dialog");
frm.setSize(200,150);
frm.setLocation(200, 200);
welcome.setAlignment(Label.CENTER);
dlg.setTitle("你确定要关闭?");
dlg.setSize(160, 120);
dlg.setLayout(new FlowLayout(FlowLayout.CENTER,5,30));
dlg.add(Close_btn);
dlg.add(Cancel_btn);
Close_btn.addActionListener((ActionListener) frm);
Cancel_btn.addActionListener((ActionListener) frm);
frm.addWindowListener(wlis);
frm.setVisible(true);
}
static class WinLis extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
dlg.setLocation(230, 200);
dlg.show();
}
}
public void actionPerformed(ActionEvent e)
{
Button btn = (Button)e.getSource();
if(btn == Close_btn)
{
dlg.dispose();
frm.dispose();
}
else if(btn == Cancel_btn);
dlg.hide();
}
}
编译没问题,运行出现如下问题:
Exception in thread "main" java.lang.ClassCastException: java.awt.Dialog cannot be cast to java.awt.event.ActionListener
at textPackage.AppletTest3.main(AppletTest3.java:38)
38,39行为:
Close_btn.addActionListener((ActionListener) frm);
Cancel_btn.addActionListener((ActionListener) frm); 展开
展开全部
很显然
Frame frm = new Frame();
说明frm是一个Frame的对象,Frame并没有实现ActionListener 这个接口,或者有继承它。那么你把它强制转换为ActionListener当然报错。相当于把石头当着馒头啃,当然就不对啦!因为石头不是馒头。
Frame frm = new Frame();
说明frm是一个Frame的对象,Frame并没有实现ActionListener 这个接口,或者有继承它。那么你把它强制转换为ActionListener当然报错。相当于把石头当着馒头啃,当然就不对啦!因为石头不是馒头。
展开全部
ClassCastException类型转换异常。Dialog怎么能转换成ActionListener呢?印象中监听器是需要自己实现的吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
一句话:你的frm不是ActionListener类型的,不能转换
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
额,你frm是个Frame类型,你咋转ACtionListener呢,都完全没道理的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询