大家帮忙看看这个日历记事本要怎么修改呀日志文件总是保存出错
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 java.io.*;
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<days;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<String,File> hm=new HashMap<String,File> ();
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按钮监听做不了的话,用鼠标监听做行吗?要怎么做呀。
----------------解决方案--------------------------------------------------------
你要问什么
请把重点贴出来
----------------解决方案--------------------------------------------------------
假设一共设置了2个按钮,那么监听器就是
button1.addActionListener(this);
button2.addActionListener(this);
在actionPerformed(ActionEvent e)函数中
if(e.getSource() == button1){……走事件1}
if(e.getSource() == button2){……走事件2}
----------------解决方案--------------------------------------------------------
我把监听定义成了一个类,用到哪个按钮就传递过去哪个按钮应的值。
----------------解决方案--------------------------------------------------------
回复 4楼 ggggwffgqeg
也行呀。不过你传递过去的值最好还是在我上面的事件1、2中,调用类函数 ----------------解决方案--------------------------------------------------------
我又回来了,现在大学毕业2年多了,看看大学时发的贴子,感觉有点傻、有点可爱、有点执着。好怀念我的大学。
而如今的我是否还有着当初的执着
[ 本帖最后由 ggggwffgqeg 于 2013-5-28 17:27 编辑 ]
----------------解决方案--------------------------------------------------------