无法将addActionListener应与于对象的问题

lie1213 2009-12-07 03:31:56
RT
我定义的所有按钮都会出现这样的问题 求高人解答
如果需要看代码 我上
CalendarTrain1.java:193:找不到符号
符号:类ActionEvent
位置:类CalendarTrain1
public void actionPerformed(ActionEvent e)
CalendatTrain1.java:84:无法将javax.swing.AbstractButton中的addActionListener应与与(CalendarTrain1)
button_del.addActionListener(this);
...全文
917 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
z2010490051 2011-10-11
  • 打赏
  • 举报
回复
到底怎么改啊,我把具体的包都导进去了,为什么还是报错啊
lie1213 2009-12-07
  • 打赏
  • 举报
回复
谢谢大家 大家真是太好了 出来了呢~ 恩 我准备再改进下 就是将日期的BUTTON声明鼠标事件 然后这样就达到记事的目的了 被点击的按钮会变色 然后每次程序启动后 自动清理前于当前时间的日志
再次谢谢大家
lie1213 2009-12-07
  • 打赏
  • 举报
回复
我一般都是放到c++的里面去做 然后复制粘贴到记事本去 我现在去试验下
bayougeng 2009-12-07
  • 打赏
  • 举报
回复
import java.awt.*;
这个语句,只能把java/awt下的类或者配置文件导进来。对于目录,是不会导入的,所以你要:
import java.awt.event.*;

另外,用文本编辑器了。初学的时候知道怎么用文本编辑器helloworld就行了。
开发的时候,用文本编辑器了效率太低了。
Kevin2305 2009-12-07
  • 打赏
  • 举报
回复
java.awt.*;
java.awt.evnet.*;
这是两个不同的包,看清楚了包名不同,只不过包名差不多而已,他们没有任何关系,没有包含和被包含的关系
lie1213 2009-12-07
  • 打赏
  • 举报
回复
这样贴上去的貌似就没有风格了呢~ 哎 现在C++里面有的东西和JAVA的东西在我脑袋里碰撞的 有时候能吧指针用到JAVA里去
your_leftH 2009-12-07
  • 打赏
  • 举报
回复
眼睛都花了
lie1213 2009-12-07
  • 打赏
  • 举报
回复
不是书里说 带有*的话 在编译的时候就会将你出现的类编译进去?难道一定要单独的显示的写出一定用了那些啊
lie1213 2009-12-07
  • 打赏
  • 举报
回复
哦~ 估计还有很多错误。 明白了,等会加上去看下
stamp80 2009-12-07
  • 打赏
  • 举报
回复
楼上正解,忘记的包好象不止一个
lie1213 2009-12-07
  • 打赏
  • 举报
回复
我全都导入了吧 难道这个要单独导入? 我的问题可能幼稚点了 见谅 大二刚接触JAVA
gentalguo 2009-12-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 kevin2305 的回复:]
忘导包了
import java.awt.event.*;
[/Quote]
还有,save的处理部分

FileWriter tofile = new FileWriter(f);
BufferedWriter out = new BufferedWriter(tofile);
out.write(text.getText(), 0, (text.getText()).length());
out.flush();
tofile.close();
out.close();

这些要处理可能出现的异常,至少用try~catch包起来
Kevin2305 2009-12-07
  • 打赏
  • 举报
回复
忘导包了
import java.awt.event.*;
lie1213 2009-12-07
  • 打赏
  • 举报
回复
import java.awt.*;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.*;
import java.awt.Font;;
import java.io.*;
public class CalendarTrain1 extends JFrame implements ActionListener {

private static final long serialVersionUID =12; //兼容性说明
//以下的组件将被放在Panel_ym中,按钮组件:(查看,今天) 标签组件(年份 月份) JComboBox下拉列表(YearBox MonthBox)
private JComboBox MonthBox=new JComboBox();
private JComboBox YearBox=new JComboBox();
private JLabel YearLabel=new JLabel("年份: ");
private JLabel MonthLabel=new JLabel("月份:");
private JButton button_ok=new JButton("查 看");
private JButton button_today=new JButton("今 天");

private JButton button_save=new JButton("保存日志"); //右侧的记事本部分 此处修改
private JButton button_del=new JButton("删除日志");
private JTextArea text=new JTextArea(20,20);

private Date now_date=new Date(); //获取今天的日期,年份,月份
private int now_year=now_date.getYear()+1900;
private int now_month=now_date.getMonth();
private boolean todayFlag=false; //是否显示今天的日期
Date firstDay; //此处修改

//用一组按钮显示日期,一共7行7列,第一行为星期的名字 button_day,button_week将放置于Panel_day面板中
private JButton[] button_day=new JButton[42];
private final String[] week={"日","一","二","三","四","五","六"};
private JButton[] button_week=new JButton[7];

private String year_int=null; //保存用户选择的年份
private int month_int; //保存用户选择的月份


public CalendarTrain1(){ //构造函数
super();
System.out.print(now_month);
this.setTitle("日 历"); //设置标题
this.init();
this.setLocation(500,300);
this.setResizable(true); //设置面板的大小不能变化
pack();
}


private void init()
{ //初始化日历
Font font=new Font("Dialog",Font.BOLD,14);
YearLabel.setFont(font);
MonthLabel.setFont(font);
button_ok.setFont(font);
button_today.setFont(font);
button_save.setFont(font); //此处修改
button_del.setFont(font);
for(int i=now_year-10;i<=now_year+20;i++){ //设定年份区间,为当前年份的过去10年到当前年份的未来20年
YearBox.addItem(i+"");
}
YearBox.setSelectedIndex(10); //设定年份下拉列表为当前年份,当前年份处于第10项
for(int i=1;i<=12;i++){ //设定月份区间,12个月
MonthBox.addItem(i+"");
}
MonthBox.setSelectedIndex(now_month); //设定月份下拉列表为当前月份

//放置下拉列表框和控制按钮的面板
JPanel panel_ym=new JPanel();
panel_ym.add(YearLabel);
panel_ym.add(YearBox);
panel_ym.add(MonthLabel);
panel_ym.add(MonthBox);
panel_ym.add(button_ok);
panel_ym.add(button_today);
panel_ym.add(button_save); //此处修改
panel_ym.add(button_del);


//为两个按钮添加事件监听器
button_del.addActionListener(this); //此处修改
button_save.addActionListener(this);


button_ok.addActionListener(this);
button_today.addActionListener(this);
JPanel panel_day=new JPanel(); //放置日期面板
panel_day.setLayout(new GridLayout(7,7,3,3)); //网格布局管理器,7行7列,网格之间水平和垂直方向间均为5
//添加星期的名字,并放到面板里面
for(int i=0;i<7;i++){
button_week[i]=new JButton(" ");
button_week[i].setText(week[i]);
button_week[i].setForeground(Color.black);
panel_day.add(button_week[i]);
}
button_week[0].setForeground(Color.red);
button_week[6].setForeground(Color.red);
//添加日期,放到面板里面
for(int i=0;i<42;i++){
button_day[i]=new JButton(" ");
panel_day.add(button_day[i]);
}
this.paintDay();//显示当前年月的日期

JPanel panel_main=new JPanel();//调用边界布局管理器 放置以上两个面板
panel_main.setLayout(new BorderLayout());
panel_main.add(panel_day,BorderLayout.SOUTH);
panel_main.add(panel_ym,BorderLayout.NORTH);
panel_main.add(text,BorderLayout.WEST); //此处修改

getContentPane().add(panel_main);
}


private void paintDay() //功能函数,用于绘制按钮等
{
if(this.todayFlag)
{
year_int=now_year+"";
month_int=now_month;
}
else
{
year_int=YearBox.getSelectedItem().toString();
month_int=MonthBox.getSelectedIndex();
}
int year_sel=Integer.parseInt(year_int)-1900;//获得年份置
Date firstDay=new Date(year_sel,month_int,1);//构造该月的第一天
GregorianCalendar cal=new GregorianCalendar();//创建一个Calendar的实例
cal.setTime(firstDay);
int days=0; //存放某个月份的天数
int day_week=0; //存放某个月份的第一天是星期几的数值

//判断是几月份,根据它来设定day的值,其中二月份要判断是否是闰年
if(month_int==0||month_int==2||month_int==4||month_int==6||month_int==7||month_int==9||month_int==11){days=31;}
else if(month_int==3||month_int==5||month_int==8||month_int==10){days=30;}
else{
if(cal.isLeapYear(year_sel)){days=29;}
else days=28;
}
day_week= firstDay.getDay(); //getDay方法获取该天是星期几
int count=1;
/*
* 绘制按钮。在这里首先要根据选定的月份的第一天是星期几来确定绘制按钮的起始位置
* 其中day_week就是我们要绘制的起始位置,对于那些没有数值可以显示的按钮要置空
*/
for(int i=day_week;i<day_week+days;count++,i++)
{
if(i%7==0||i==6||i==13||i==20||i==27||i==34||i==41) //如果是边界上的按钮,文字用红色,以来标示周末
{
if(i==day_week+now_date.getDate()-1) //将与今天一样的日期用蓝色标示
{
button_day[i].setForeground(Color.blue);
button_day[i].setText(count+"");
}
else
{ //其它 边界上的按钮中的文字用红色
button_day[i].setForeground(Color.red);
button_day[i].setText(count+"");
}
}
else
{
if(i==day_week+now_date.getDate()-1)
{
button_day[i].setForeground(Color.blue);
button_day[i].setText(count+"");
}
else
{
button_day[i].setForeground(Color.black); //一般位置的按钮上的文字用黑色标示
button_day[i].setText(count+"");
}
}

}

if(day_week==0)
{ //对于没有日期数值显示的按钮进行置空处理
for(int i=days;i<42;i++){button_day[i].setText(" ");} //如果第一天是周日,则将后面的按钮上的文字都设为空
}
else
{
for(int i=0;i<day_week;i++){button_day[i].setText(" ");} //如果第一天不是周日,则将第一天前面的按钮置空
}

}


public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button_ok) //如果单击“查看”按钮就调用setDay()重新方法绘制按钮
{
this.todayFlag=false;
this.paintDay();
}
else if(e.getSource()==button_today) //如果单击“今天”按钮,得到今天的日期
{
todayFlag=true;
YearBox.setSelectedIndex(10);
MonthBox.setSelectedIndex(now_month);
this.paintDay();
}





else if(e.getSource()==button_save) //此处修改处理按钮方法
{
String str=text.getText();
File f=new File(text.getParent().toString()+str+".txt");
FileWriter tofile=new FileWriter(f);
BufferedWriter out=new BufferedWriter(tofile);
out.write(text.getText(),0,(text.getText()).length());
out.flush();
tofile.close();
out.close();

}
else if(e.getSource()==button_del)
{
String str=text.getText();
File f=new File(text.getParent().toString()+str+".text");
f.delete();

}
}

/**
* @param args
*/
public static void main(String[] args)
{
CalendarTrain1 CT=new CalendarTrain1();
CT.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CT.setVisible(true);
//ct.show();
}
}
bayougeng 2009-12-07
  • 打赏
  • 举报
回复
上代码。
bean 2009-12-07
  • 打赏
  • 举报
回复
用你声明的button对象addActionListener

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧