记事本编辑java文件出错_大家帮忙看看这个日历记事本要怎么修改呀日志文件总是保存出错...

已结贴√

问题点数:0 回复次数:5

ca56232b3bbedf9a539d07f37fffb99a.gif

3144d8b7615c79d9f638db40d5689d26.gif

a218af6549b45ee526caf607ebff1358.gif

0f8df0e29816ae721419de940fb833d1.gif

大家帮忙看看这个日历记事本要怎么修改呀日志文件总是保存出错

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.util.Calendar;

import java.util.HashMap;

import *;

import javax.swing.*;

public class CalendarPad {

public static void main(String args[]) {

NoteFrame nf=new NoteFrame();

}

}

class NoteFrame implements ActionListener{

JFrame jf;

JPanel jp1,jp2,jp3,jp4,jp5;

JButton upYear, downYear,upMonth,downMonth,saveLog,deleteLog;

Button[] day;

JLabel[] week;

JTextField year,month;

static TextArea contentLog;

static Label help,date;

JSplitPane js;

int y,m,d;

JScrollPane jsp;

Boolean isFirst=true;

String logName;//用以保存日志的日志名称;

public NoteFrame(){

setJp1();

setJp2();

setJp3();

setJp5();

isFirst=false;

jp4=new JPanel();

jp4.setSize(350, 400);

jp4.add(jp1);

jp4.add(jp2);

jp4.add(jp3);

js=new JSplitPane();

js.setDividerLocation(350);

js.setTopComponent(jp4);

js.setBottomComponent(jp5);

jf=new JFrame();

jf.setTitle("日历记事本");

jf.add(js);

jf.setSize(650,400);

jf.setVisible(true);

jf.addWindowListener(new WindowHandler());

Container ct=jf.getContentPane();

ct.setLayout(null);

}

public void setJp1(){

upYear=new JButton("上年");

upYear.setSize(60,40);

upYear.addActionListener(this);

year=new JTextField("2008",JTextField.CENTER );

year.setSize(50,40);

year.setColumns(3);

downYear=new JButton("下年");

downYear.setSize(60,40);

downYear.addActionListener(this);

upMonth=new JButton("上月");

upMonth.setSize(60,40);

upMonth.addActionListener(this);

month=new JTextField("1",JTextField.CENTER );

month.setColumns(3);

month.setSize(50,40);

downMonth=new JButton("下月");

downMonth.setSize(60,40);

downMonth.addActionListener(this);

jp1=new JPanel();

jp1.setSize(350,50);

jp1.add(upYear);

jp1.add(year);

jp1.add(downYear);

jp1.add(upMonth);

jp1.add(month);

jp1.add(downMonth);

}

//设置星期

public void setJp2(){

String[] wk={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};

jp2=new JPanel();

jp2.setSize(350,100);

jp2.setVisible(true);

GridLayout gl=new GridLayout(1,7);

gl.setVgap(10);

gl.setHgap(9);

jp2.setLayout(gl);

week=new JLabel[7];

for(int i=0;i<7;i++){

week[i]=new JLabel(wk[i]);

week[i].setSize(50,40);

jp2.add(week[i]);

}

week[0].setForeground(Color.RED);

week[6].setForeground(Color.BLUE);

}

public void setJp3(){

if(isFirst){

jp3=new JPanel();

jp3.setSize(350,110);

GridLayout gl=new GridLayout(6,7);

gl.setVgap(5);

gl.setHgap(3);

jp3.setLayout(gl);

}

int getYear=Integer.parseInt(year.getText());  //得到年

int getMonth=Integer.parseInt(month.getText());    //得到月

Calendar cl=Calendar.getInstance();

cl.set(getYear,getMonth-1,1);

//大月

if(getMonth==1 || getMonth==3 || getMonth==5 || getMonth==7 || getMonth==8 || getMonth==10 || getMonth==12){

showCalendar(cl.get(cl.DAY_OF_WEEK),31);

}

//小月

else if(getMonth==4 || getMonth==6 || getMonth==9 || getMonth==11){

showCalendar(cl.get(cl.DAY_OF_WEEK),30);

}

//是否是瑞年

else if(getMonth==2){

if((getYear%4==0 && getYear%100!=0) || getYear%400==0){

showCalendar(cl.get(cl.DAY_OF_WEEK),29);

}

else{

showCalendar(cl.get(cl.DAY_OF_WEEK),28);

}

}

}

public void showCalendar(int w,int days){

//判断是否是第一次加载

if(isFirst){

day=new Button[42];

Font ft=new Font("黑体",Font.PLAIN,28); //设置字体

for(int i=0;i<42;i++){

day[i]=new Button("");

day[i].setFont(ft);

jp3.add(day[i]);

if(i%7==0 || i==0){

day[i].setForeground(Color.RED);

}

if(i%7==6){

day[i].setForeground(Color.blue);

}

}

}

else{ //清空Buuton内容

for(int i=0;i<42;i++){

day[i].setLabel(null);

day[i].removeActionListener(this);

}

}

//设置月的第一天是从星期几开始

for(int i=0;i

day[i+w-1].setLabel( String.valueOf(1+i));

logName=year.getText()+month.getText()+day[i+w-1].getLabel(); //设置日志名

day[i+w-1].addActionListener(new ButtonActionListener(logName));

}

}

public void setJp5(){

if(isFirst){

jp5=new JPanel();

jp5.setSize(300,400);

jp5.setVisible(true);

Calendar cl=Calendar.getInstance();

Font ft=new Font("黑体",Font.BOLD,20);

date=new Label(String.valueOf(year.getText()+" 年 "+month.getText()+" 月 "+"1"+" 日"));

date.setFont(ft);

date.setSize(300,50);

saveLog=new JButton("保存日志");

saveLog.setSize(80,40);

saveLog.addActionListener(new Log("saveLog",logName));//hfghfhgfhgfhfytytythghg

deleteLog=new JButton("删除日志");

deleteLog.setSize(80,40);

deleteLog.addActionListener(new Log("deleteLog",logName));

contentLog=new TextArea(15,28);

contentLog.setFont(new Font("宋体",Font.PLAIN,12));

contentLog.setText("");

jp5.add(date);

jp5.add(contentLog);

jp5.add(saveLog);

jp5.add(deleteLog);

}

}

public void actionPerformed(ActionEvent e) {

int getYear=Integer.parseInt(year.getText());

int getMonth=Integer.parseInt(month.getText());

if(e.getSource().equals(upYear)){

year.setText(String.valueOf(getYear-1));

setJp3();

}

else if(e.getSource().equals(downYear)){

year.setText(String.valueOf(getYear+1));

setJp3();

}

else if(e.getSource().equals(downMonth)){

if((getMonth+1)==13){

year.setText(String.valueOf(getYear+1));

month.setText(String.valueOf(1));

}

else{

month.setText(String.valueOf(getMonth+1));

}

setJp3();

}

else if(e.getSource().equals(upMonth)){

if((getMonth-1)==0){

getYear--;

getMonth=12;

year.setText(String.valueOf(getYear));

month.setText(String.valueOf(getMonth));

}

else{

month.setText(String.valueOf(getMonth-1));

}

setJp3();

}

date.setText(String.valueOf(year.getText()+" 年 "+month.getText()+" 月 "+"1"+" 日"));

}

}

class WindowHandler extends WindowAdapter{

public void windowClosing(WindowEvent e){

System.exit(1);

}

}

class ButtonActionListener implements ActionListener{

String number;

File file;

public ButtonActionListener(String i){

this.number=i;              //总是传递过去2008131.txt 按钮监听怎么做呀

System.out.println(i);

}

public void actionPerformed(ActionEvent e) {

String str;

if((file=Log.hm.get(number))!=null){  //如果有日志

if(file.isFile()){ //判断是否有文件;

try{

BufferedReader br=new BufferedReader(new FileReader(file.getAbsolutePath()));

System.out.println(file.getAbsolutePath());

NoteFrame.contentLog.setText(null);

while((str=br.readLine())!=null){

NoteFrame.contentLog.append(str+"\n");

}

br.close();

}catch(IOException e1){NoteFrame.contentLog.setText("日志打开时出现错误");

NoteFrame.contentLog.setForeground(Color.red);}

}

else{

NoteFrame.contentLog.setText("日志可能已被非正常删除");

NoteFrame.contentLog.setForeground(Color.red);

}

}

else{

NoteFrame.contentLog.setText("没有日志");

}

}

}

//日志操作监听

class Log implements ActionListener{

String event;

String logName;

File file;

static HashMap hm=new HashMap ();

public Log(String event,String logName){

this.event=event;

this.logName=logName;   //总是传递过去2008131.txt 按钮监听怎么做呀。

}

public void actionPerformed(ActionEvent e) {

if(event.equals("saveLog")){

try{

file=new File(logName+".txt");  //创建日志文件

file.createNewFile();

BufferedWriter bw=new BufferedWriter(new FileWriter(logName+".txt"));

bw.write(NoteFrame.contentLog.getText());

bw.flush();

bw.close();

}catch(IOException e1){NoteFrame.contentLog.setText("日志保存时出现错误");NoteFrame.contentLog.setForeground(Color.red);}

hm.put(logName, file);

}

else if(event.equals("deleteLog")){

file=new File(logName+".txt");

if(file.isFile()){

file.delete();

NoteFrame.contentLog.setForeground(Color.red);

NoteFrame.contentLog.setText("日志删除成功");

hm.remove(logName);

}

else{

NoteFrame.contentLog.setForeground(Color.red);

NoteFrame.contentLog.setText("没有日志文件");

}

}

}

}

每次保存都把文件保存到了名为2008131.txt的文件。Button按钮总做不监听大家帮忙修改一下。

如果Button按钮监听做不了的话,用鼠标监听做行吗?要怎么做呀。

63d3be0d86606934519d75473fe4cfc2.gif

63d3be0d86606934519d75473fe4cfc2.gif

63d3be0d86606934519d75473fe4cfc2.gif

搜索更多相关主题的帖子:

保存 日志 文件 记事本 日历

计算机科学与技术本科专业 Java课程设计任务书 题目: 日历记事本 学生姓名: 学号: 班级: 题目类型:软件工程(R) 指导教师: 一. 题目简介 该设计要求设计一个带有日程提醒功能的日历。 通过该题目的设计,培养学生面向对象程序设计的思想,要求学生达到熟练掌握Java语言的基本知识和技能,基本掌握面向对象程序设计的基本思路和方法,能够利用所学的基本知识和技能,解决简单的面向对象程序设计问题。同时强调好的程序设计风格,提高解决实际问题的能力。 二. 主要任务 1、查阅文献资料,一般在5篇以上; 2、应用系统分析,建立该系统的功能模块框图以及界面的组织和设计; 3、分析系统中的各个实体及它们之间的关系包括属性和方法; 4、根据问题描述,设计系统的类层次; 5、完成类层次中各个类的描述(包括属性和方法); 6、完成类中各个成员函数的定义; 7、完成系统的应用模块; 8、完成系统的软件开发和测试工作; 9、撰写设计说明书; 10、做好答辩工作。 三. 主要内容、功能及技术指标 (一) 基本要求: 编写一个Applet小应用程序能够显示时间,当用户最小化Applet时,程序会 被挂起,当还原Applet时,程序又会恢复执行,同时Applet中应显示挂起和恢复的时间。 (二)系统要求: 1、要求利用面向对象的方法以及Java的编程思想来完成系统的设计; 2、要求在设计的过程中,建立清晰的类层次; 3、在系统设计中要分析和定义各个类,每个类中要有各自的属性和方法; 4、在系统的设计中,要求运用面向对象的机制(继承、派生及多态性)来实现系统功能。 5、该系统的要求: (1)显示信息:用户可以向前翻页查询前一个月的日期,也可以向后翻页查询下一个月的日期。 (2)定时提醒:用户可以针对某一天来添加,删除和编辑这一天的日程提醒信息,当系统时间和提醒时间相吻合时,给出具有提示信息的对话框; (3)查询信息:用户可以查询到某个月的所有提示信息。 四. 提交的成果 1. 设计说明书一份,内容包括: 1) 中文摘要100字;关键词3-5个; 2) 序言; 3) 基本要求实现(包括程序流程图;部分代码;) 4) 系统要求实现 系统分析与设计(包括设计系统的类层次,各个实体及它们之间的关系) 详细设计(包括各个类的详细设计,如数据成员及成员函数的设计); 系统测试(包含测试方案、测试用例、测试结果及软件可靠性分析); 软件使用说明书(核心界面说明); 5)设计总结、参考文献、致谢等。 五. 主要参考文献 [1]曲朝阳,杨杰明等.Java程序设计. 北京: 清华大学出版社,2008.10 [2]耿祥以,张跃平.Java大学生实用教程. 北京: 电子工业出版社,2012.6 [3]明日科技.Java经典编程.北京:清华大学出版社,2012.10 [4]李尊朝,苏军.Java语言程序设计.北京:中国铁道出版社,2004.2 [5]王博. 面向对象的建模、设计技术与方法. 北京希望电脑公司,1990. 六. 各阶段时间安排(共2周): 周次 日期 内容 地点 第1周 星期一 教师讲解设计要求,准备参考资料 教室 星期二 分析系统,方案设计 实验室 星期三~五 编程 实验室 第2周 星期一~三 编程、调试程序 实验室 星期四 写设计书 实验室 星期五 答辩 实验室
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值