标题:关于一个备忘录的课程设计...io输出部分
只看楼主
qasdfgt
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-6-29
 问题点数:0 回复次数:1 
关于一个备忘录的课程设计...io输出部分
本人在做课程设计..
想做一个备忘录...
现在把输入部分完成了..(不过有很严重的缺陷...请帮忙解决)
但是输出部分不会写,就是写到FileReader one=new FileReader("Memo.BOX");这里
想把文件里的东西按“发生日期 发生时间 发生事件”这样子分别在jtable中输出(个人觉得用io和字符串的知识就能够顺利输出了...)
但是这部分不会写.....能够请大家帮忙一下吗?
老师说用sql的部分能够写...但是上课完全没有学过sql的...大后天就交了..
后天还得出外做另一个课程设计..希望大家能帮忙一下..
无尽感激...



import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.util.*;
import java.util.Calendar;
import *;

class liangbing extends JFrame implements ActionListener
{
    MenuBar menubar;
    Menu menu1,menu2,menu3;
    MenuItem item1,item2,item3,item4;
    Button button1;
    Panel panel2;
    MyDialog2 dialog2;
    MyDialog3 dialog3;
    JPanel contentPane;
    JTable table;
    Object a[][];
    Object name[]={"日期","时间","事件"};
    String path = "D:/Calendar_Memo/Memo.BOX";
    File files=null;
    

    liangbing(String s)
    {
        setTitle(s);
        Toolkit tool=getToolkit();
        Dimension dim=tool.getScreenSize();
        setBounds(dim.width/3,dim.height/3,550,350);
        
        contentPane=(JPanel)getContentPane();
        a=new Object[8][3];
        for(int i=0;i<8;i++)
        {
            for(int j=0;j<3;j++)
            {
                a[i][j]="0";
            }
        }
        
        table=new JTable(a,name);
        menubar=new MenuBar();
        panel2=new Panel();
        menu1=new Menu("文件");
        menu2=new Menu("编辑");
        menu3=new Menu("帮助");
        item1=new MenuItem("退出");
        item2=new MenuItem("增加");
        item3=new MenuItem("删除");
        item4=new MenuItem("关于");
        button1=new Button("刷新");
        dialog2=new MyDialog2(this,"增加",false);
        dialog3=new MyDialog3(this,"删除",true);
        
        panel2.setBackground(Color.gray);
        
        button1.addActionListener(this);
        item1.addActionListener(this);
        item4.addActionListener(this);
        item2.addActionListener(this);
        item3.addActionListener(this);
        
        setMenuBar(menubar);
        menubar.add(menu1);
        menubar.add(menu2);
        menubar.add(menu3);
        menu1.add(item1);
        menu2.add(item2);
        menu2.add(item3);
        menu3.add(item4);
        panel2.add(button1);
        contentPane.add(new JScrollPane(table));
    add(panel2,BorderLayout.SOUTH);
   
    table.getColumnModel().getColumn(0).setPreferredWidth(80);
    table.getColumnModel().getColumn(1).setPreferredWidth(70);
    table.getColumnModel().getColumn(2).setPreferredWidth(400);
   
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    validate();
    }//1024*768

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==button1)
        {
            //try
            {
                if (files==null)
        {
          files=new File(path);
        }
        FileReader one=new FileReader("Memo.BOX");
        
            }
            //catch(IOException g)
            //{
                //System.out.println("File read Error"+g);
            //}
            validate();
        }
        if(e.getSource()==item1)
        {
            dispose();
      System.exit(0);
        }
        if(e.getSource()==item4)
        {
            JOptionPane.showMessageDialog(this,"本軟件由XXX熱情製作,專利所有,請勿copy","關於",JOptionPane.INFORMATION_MESSAGE);
        }
        if(e.getSource()==item2)
        {
            dialog2.setVisible(true);
        }
        if(e.getSource()==item3)
        {
            dialog3.setVisible(true);
        }
    }
}

class MyDialog2 extends Dialog implements ActionListener,ItemListener
{
    Panel controltime,controldates,controlnorth,controldatemeno,controltimes,controlpanel;
    Label starttime,years,months,days,符号1,dates,startmeno,yyy,mmm,ddd;
    TextField yearss,monthss,dayss,starthour,starminute,yearssss,monthssss,dayssss;
    JTextArea note;
    Button ok,cancel,choose1,choose2;
    Checkbox longtime;
    Calendar calendar;
    Boolean longyear=false;
    Boolean diaoyong=false;
    File file=null;
    String date;
    String datemeno;
    String writeFile=new String();
    String readFile = new String();
    String memo;
    private static final String path="D:/Calendar_Memo/Memo.BOX";
    
    MyDialog2(JFrame f,String s,boolean b)
    {
        super(f,s,b);
        Toolkit tool=getToolkit();
        Dimension dim=tool.getScreenSize();
        setBounds((dim.width/3)+50,(dim.height/3)+50,410,315);//65
        
        longtime=new Checkbox("年周期节日");
        choose1=new Button("选择");
        choose2=new Button("选择");
        ok=new Button("确定");
        cancel=new Button("取消");
        yearssss=new TextField(10);
        monthssss=new TextField(2);
        dayssss=new TextField(2);
        yearss=new TextField(10);
        monthss=new TextField(2);
        dayss=new TextField(2);
        starthour=new TextField(2);
        starminute=new TextField(2);
        starttime=new Label("发生时间:");
        startmeno=new Label("提醒日期:");
        years=new Label("年");
        months=new Label("月");
        days=new Label("日");
        yyy=new Label("年");
        mmm=new Label("月");
        ddd=new Label("日");
        符号1=new Label(":");
        dates=new Label("日期:");
        note=new JTextArea("请输入要记入的事情",4,20);
        note.setLineWrap(true);
        controlnorth=new Panel();
        controldatemeno=new Panel();
        controlpanel=new Panel();
        controldates=new Panel();
        controltimes=new Panel();
        controltime=new Panel();
        controltime.setLayout(new BorderLayout());
        controlnorth.setLayout(new BorderLayout());
        controlnorth.setBounds(0,0,410,130);
        
        controltimes.add(starttime);
        controltimes.add(starthour);
        controltimes.add(符号1);
        controltimes.add(starminute);
        controltimes.add(longtime);
        
        controldates.add(dates);
        controldates.add(yearss);
        controldates.add(years);
        controldates.add(monthss);
        controldates.add(months);
        controldates.add(dayss);
        controldates.add(days);
        controldates.add(choose1);
        
        controlpanel.add(ok);
        controlpanel.add(cancel);
        
        controldatemeno.add(startmeno);
        controldatemeno.add(yearssss);
        controldatemeno.add(yyy);
        controldatemeno.add(monthssss);
        controldatemeno.add(mmm);
        controldatemeno.add(dayssss);
        controldatemeno.add(ddd);
        controldatemeno.add(choose2);
        
        controlnorth.add(controldates,BorderLayout.NORTH);
        controlnorth.add(controldatemeno,BorderLayout.SOUTH);
        controltime.add(controlnorth,BorderLayout.NORTH);
        controltime.add(controltimes,BorderLayout.SOUTH);
        add(controltime,BorderLayout.NORTH);
        add(note,BorderLayout.CENTER);
        add(controlpanel,BorderLayout.SOUTH);
        ok.addActionListener(this);
        choose2.addActionListener(this);
        choose1.addActionListener(this);
        cancel.addActionListener(this);
        longtime.addItemListener(this);
        
        addWindowListener(new WindowAdapter()
                      {
                          public void windowClosing(WindowEvent e)
                          {
                              dispose();
                          }
                      });
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==choose2)
        {
          calendar=Calendar.getInstance();   
      int y=calendar.get(Calendar.YEAR);           
      int m=calendar.get(Calendar.MONTH)+1;        
      int d=calendar.get(Calendar.DAY_OF_MONTH);
      new CalendarPad(y,m,d,this);
      diaoyong=false;
    }
        if(e.getSource()==choose1)
        {
          calendar=Calendar.getInstance();   
      int y=calendar.get(Calendar.YEAR);           
      int m=calendar.get(Calendar.MONTH)+1;        
      int d=calendar.get(Calendar.DAY_OF_MONTH);
      new CalendarPad(y,m,d,this);
      diaoyong=true;
    }
    else if(e.getSource()==cancel)
    {
        dispose();
    }
    if(e.getSource()==ok)
    {
        try
        {
            int yearsss,mouthsss,daysss,starthours,starminutes;
            yearsss=Integer.parseInt(yearss.getText());
            mouthsss=Integer.parseInt(monthss.getText());
            daysss=Integer.parseInt(dayss.getText());
            starthours=Integer.parseInt(starthour.getText());
            starminutes=Integer.parseInt(starminute.getText());
            StringBuffer str=new StringBuffer(yearss.getText());
            str.append(monthss.getText());
            str.append(starthour.getText());
            str.append(starminute.getText());
            date=new String(str);
            StringBuffer dtr=new StringBuffer(yearssss.getText());
            dtr.append(monthssss.getText());
            dtr.append(dayssss.getText());
            datemeno=new String(dtr);
            memo=note.getText().trim();
            if(memo.length()== 0)
        {
          JOptionPane.showMessageDialog(this,"备忘录内容不能为空");
          return;
        }
            if((yearsss>9999&&mouthsss>12&&mouthsss<1&&daysss>31&&daysss<1&&starthours<1&&starthours>24&&starminutes<1&&starminutes>60)==false)
            {
                try
                {
                  if (file==null)
            {
              file=new File(path);
            }
            BufferedReader bf=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            while((readFile=bf.readLine())!=null)
            {   
              writeFile+=readFile+"\r\n";
            }
            bf.close();
            writeFile+=date+ ","+datemeno+","+memo;
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));
            bw.write(writeFile);
            bw.close();
            JOptionPane.showMessageDialog(this, "已保存");
            dispose();
          }
          catch (IOException s)
          {
            System.out.println("------" + s.getMessage() + "------");
          }
        }
        }
        catch(Exception ee)
        {
            note.append("请输入正确的字符");
        }
    }
    }
    
    public void itemStateChanged(ItemEvent e)
    {
        Checkbox box=(Checkbox)e.getSource();
        if(box.getState())
        {
            longyear=true;
        }
    }
    public void 设置日期(int year,int month,int day)
    {
        if(diaoyong==true)
        {
          yearss.setText(""+year);
          monthss.setText(""+month);
          dayss.setText(""+day);
        }
        if(diaoyong==false)
        {
          yearssss.setText(""+year);
          monthssss.setText(""+month);
          dayssss.setText(""+day);
        }
    }
    public void 显示错误()
    {
        yearss.setText("请点击日期");
        monthss.setText("");
        dayss.setText("");
    }
}

class MyDialog3 extends Dialog
{
    Button delete;
    JTable table;
    Object a[][];
    Object name[]={"日期","时间","事件"};
    Panel panel1,panel3;
    Label label1,label2,label3;
    
    MyDialog3(JFrame f,String s,boolean b)
    {
        super(f,s,b);
        Toolkit tool=getToolkit();
        Dimension dim=tool.getScreenSize();
        setBounds((dim.width/3)+50,(dim.height/3)+50,350,250);
        
        a=new Object[8][3];
        for(int i=0;i<8;i++)
        {
            for(int j=0;j<3;j++)
            {
                a[i][j]="0";
            }
        }
        
        table=new JTable(a,name);
        add(table,BorderLayout.CENTER);
        table.getColumnModel().getColumn(0).setPreferredWidth(80);
    table.getColumnModel().getColumn(1).setPreferredWidth(65);
    table.getColumnModel().getColumn(2).setPreferredWidth(205);
   
    label1=new Label("日期",Label.LEFT);
    label2=new Label("时间",Label.LEFT);
    label3=new Label("事情",Label.LEFT);
    label1.setBounds(0,0,80,18);
    label2.setBounds(80,0,65,18);
    label3.setBounds(145,0,205,18);
    panel1=new Panel();
    panel1.setBounds(0,0,350,18);
    panel1.setLayout(null);
    panel3=new Panel();
    delete=new Button("删除");
   
    panel1.add(label1);
    panel1.add(label2);
    panel1.add(label3);
    panel3.add(delete);
   
    add(panel1,BorderLayout.NORTH);
    add(panel3,BorderLayout.SOUTH);
        
        addWindowListener(new WindowAdapter()
                      {
                          public void windowClosing(WindowEvent e)
                          {
                              setVisible(false);
                          }
                      });
    }
}

class CalendarPad extends JFrame implements MouseListener
{
  int year,month,day;                           
  JButton showDay[];            
  JLabel title[];                  
  Calendar 日历;
  int 星期几;         
  Month 负责改变月;
  Year  负责改变年;
  String 星期[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
  JPanel leftPanel;
  MyDialog2 dialog2;
  public  CalendarPad(int year,int month,int day,MyDialog2 dialog2)
  {
       this.dialog2=dialog2;
    leftPanel=new JPanel();
    JPanel leftCenter=new JPanel();
     JPanel leftNorth=new JPanel();
     leftCenter.setLayout(new GridLayout(7,7));   
     this.year=year;
     this.month=month;
     this.day=day;
     负责改变年=new Year(this);
     负责改变年.setYear(year);
     负责改变月=new Month(this);
     负责改变月.setMonth(month);
     title=new JLabel[7];                        
     showDay=new JButton[42];               
     for(int j=0;j<7;j++)                        
       {
         title[j]=new JLabel();
         title[j].setText(星期[j]);
         title[j].setBorder(BorderFactory.createRaisedBevelBorder());
         leftCenter.add(title[j]);
       }
     title[0].setForeground(Color.red);
     title[6].setForeground(Color.blue);

     for(int i=0;i<42;i++)                        
       {
         showDay[i]=new JButton();
         showDay[i].addMouseListener(this);
         leftCenter.add(showDay[i]);
       }   
     日历=Calendar.getInstance();
     Box box=Box.createHorizontalBox();         
     box.add(负责改变年);
     box.add(负责改变月);
     leftNorth.add(box);
     leftPanel.setLayout(new BorderLayout());
     leftPanel.add(leftNorth,BorderLayout.NORTH);
     leftPanel.add(leftCenter,BorderLayout.CENTER);
     leftPanel.add(new Label("请在年份输入框输入所查年份(负数表示公元前),并回车确定"),
                  BorderLayout.SOUTH) ;
     leftPanel.validate();
     add(leftPanel,BorderLayout.CENTER);
     设置日历牌(year,month);
    setVisible(true);
    setBounds(100,50,410,285);
    validate();
   }
  public void 设置日历牌(int year,int month)
   {
     日历.set(year,month-1,1);              
     
     星期几=日历.get(Calendar.DAY_OF_WEEK)-1;
     if(month==1||month==2||month==3||month==5||month==7
                        ||month==8||month==10||month==12)
        {
            排列号码(星期几,31);         
        }
     else if(month==4||month==6||month==9||month==11)
        {
            排列号码(星期几,30);
        }
     else if(month==2)
        {
         if((year%4==0&&year%100!=0)||(year%400==0))  
           {
             排列号码(星期几,29);
           }
         else
           {
             排列号码(星期几,28);
           }
       }
   }
  public void 排列号码(int 星期几,int 月天数)
   {
      for(int i=星期几,n=1;i<星期几+月天数;i++)
             {
                  Calendar calendar=Calendar.getInstance();
               calendar.setTime(new Date());
               int m=calendar.get(Calendar.MONTH)+1;
               showDay[i].setText(""+n);
               if(m==month&&n==day)
               {
                    showDay[i].setForeground(Color.green);
                 showDay[i].setFont(new Font("TimesRoman",Font.BOLD,20));
               }
               else
               {
                 showDay[i].setFont(new Font("TimesRoman",Font.BOLD,12));
                 showDay[i].setForeground(Color.black);
               }
               if(i%7==6)
                 {
                  showDay[i].setForeground(Color.blue);  
                 }
               if(i%7==0)
                 {
                  showDay[i].setForeground(Color.red);  
                 }
               n++;
             }
       for(int i=0;i<星期几;i++)
             {
                showDay[i].setText("");
             }
       for(int i=星期几+月天数;i<42;i++)
             {
                showDay[i].setText("");
             }
   }
 public int getYear()
   {
    return year;
   }
public void setYear(int y)
   {
     year=y;
   }
 public int getMonth()
   {
    return month;
   }
public void setMonth(int m)
   {
     month=m;
   }
 public int getDay()
   {
    return day;
   }
 public void mousePressed(MouseEvent e)            
   {
     JButton source=(JButton)e.getSource();
     try{
         day=Integer.parseInt(source.getText());
         dialog2.设置日期(year,month,day);
         setVisible(false);
        }
      catch(Exception ee)
        {
            dialog2.显示错误();
            setVisible(false);
        }
   }
 public void mouseClicked(MouseEvent e)
   {
   }
 public void mouseReleased(MouseEvent e)
   {
   }
 public void mouseEntered(MouseEvent e)
   {
   }
 public void mouseExited(MouseEvent e)
   {
   }
}
class Year extends Box implements ActionListener
{
  int year;                           
  JTextField showYear=null;           
  JButton 明年,去年;
  CalendarPad 日历;
  public Year(CalendarPad 日历)
  {  
     super(BoxLayout.X_AXIS);        
     showYear=new JTextField(4);
     showYear.setForeground(Color.blue);
     showYear.setFont(new Font("TimesRomn",Font.BOLD,14));
     this.日历=日历;
     year=日历.getYear();
     明年=new JButton("下年");
     去年=new JButton("上年");
     add(去年);
     add(showYear);
     add(明年);
     showYear.addActionListener(this);
     去年.addActionListener(this);
     明年.addActionListener(this);
  }
 public void setYear(int year)
  {
    this.year=year;
    showYear.setText(""+year);
  }
 public int getYear()
  {
    return year;
  }
 public void actionPerformed(ActionEvent e)
  {
    if(e.getSource()==去年)
      {
        year=year-1;
        showYear.setText(""+year);
        日历.setYear(year);
        日历.设置日历牌(year,日历.getMonth());
      }
    else if(e.getSource()==明年)
      {
        year=year+1;
        showYear.setText(""+year);
        日历.setYear(year);
        日历.设置日历牌(year,日历.getMonth());
      }
    else if(e.getSource()==showYear)
      {
         try
            {
              year=Integer.parseInt(showYear.getText());
              showYear.setText(""+year);
              日历.setYear(year);
              日历.设置日历牌(year,日历.getMonth());
            }
         catch(NumberFormatException ee)
            {
              showYear.setText(""+year);
              日历.setYear(year);
              日历.设置日历牌(year,日历.getMonth());
            }
      }
  }   
}

class Month extends Box implements ActionListener
{
  int month;                           
  JTextField showMonth=null;           
  JButton 下月,上月;
  CalendarPad 日历;
  public Month(CalendarPad 日历)
  {  
     super(BoxLayout.X_AXIS);        
     this.日历=日历;
     showMonth=new JTextField(2);
     month=日历.getMonth();
     showMonth.setEditable(false);
     showMonth.setForeground(Color.blue);
     showMonth.setFont(new Font("TimesRomn",Font.BOLD,16));
     下月=new JButton("下月");
     上月=new JButton("上月");
     add(上月);
     add(showMonth);
     add(下月);
     上月.addActionListener(this);
     下月.addActionListener(this);
     showMonth.setText(""+month);
  }
 public void setMonth(int month)
  {
    if(month<=12&&month>=1)
      {
       this.month=month;
      }
    else
      {
        this.month=1;
      }
    showMonth.setText(""+month);
  }
 public int getMonth()
  {
    return month;
  }
 public void actionPerformed(ActionEvent e)
  {
    if(e.getSource()==上月)
      {
        if(month>=2)
         {
            month=month-1;
            日历.setMonth(month);
            日历.设置日历牌(日历.getYear(),month);
         }
        else if(month==1)
         {
            month=12;
            日历.setMonth(month);
            日历.设置日历牌(日历.getYear(),month);
         }
       showMonth.setText(""+month);
      }
    else if(e.getSource()==下月)
      {
        if(month<12)
          {
            month=month+1;
            日历.setMonth(month);
            日历.设置日历牌(日历.getYear(),month);
          }
        else if(month==12)
          {
            month=1;
            日历.setMonth(month);
            日历.设置日历牌(日历.getYear(),month);
          }
        showMonth.setText(""+month);
      }
  }   
}

public class liangmin
{
    public static void main(String args[])
    {
        liangbing liang=new liangbing("备忘录");
    }
}
搜索更多相关主题的帖子: import 课程 备忘录 java awt 
2008-06-29 16:07
netstriker
Rank: 1
等 级:新手上路
威 望:1
帖 子:257
专家分:0
注 册:2007-3-24
得分:0 
已经帮你改了一下,改的地方有注释的。希望可以帮到你
还有的是,以便定义变量的时候,如果用英文尽量不要用中文来定义,因为我看起来感觉很怪....
 
package com.test;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.TableColumn;

import java.util.*;
import *;

class liangbing extends JFrame implements ActionListener
{
    MenuBar menubar;
    Menu menu1,menu2,menu3;
    MenuItem item1,item2,item3,item4;
    Button button1;
    Panel panel2;
    MyDialog2 dialog2;
    MyDialog3 dialog3;
    JPanel contentPane;
    JTable table;
    Object a[][];
    Object name[]={"日期","时间","事件"};
    String path = "D:/Calendar_Memo/Memo.BOX";
    File files=null;
   

    liangbing(String s)
    {
        setTitle(s);
        Toolkit tool=getToolkit();
        Dimension dim=tool.getScreenSize();
        setBounds(dim.width/3,dim.height/3,550,350);
        
        contentPane=(JPanel)getContentPane();
        a=new Object[8][3];
        for(int i=0;i<8;i++)
        {
            for(int j=0;j<3;j++)
            {
                a[i][j]="0";
            }
        }
        
        table=new JTable(a,name);
        menubar=new MenuBar();
        panel2=new Panel();
        menu1=new Menu("文件");
        menu2=new Menu("编辑");
        menu3=new Menu("帮助");
        item1=new MenuItem("退出");
        item2=new MenuItem("增加");
        item3=new MenuItem("删除");
        item4=new MenuItem("关于");
        button1=new Button("刷新");
        dialog2=new MyDialog2(this,"增加",false);
        dialog3=new MyDialog3(this,"删除",true);
        
        panel2.setBackground(Color.gray);
        
        button1.addActionListener(this);
        item1.addActionListener(this);
        item4.addActionListener(this);
        item2.addActionListener(this);
        item3.addActionListener(this);
        
        setMenuBar(menubar);
        menubar.add(menu1);
        menubar.add(menu2);
        menubar.add(menu3);
        menu1.add(item1);
        menu2.add(item2);
        menu2.add(item3);
        menu3.add(item4);
        panel2.add(button1);
        contentPane.add(new JScrollPane(table));
    add(panel2,BorderLayout.SOUTH);
   
    table.getColumnModel().getColumn(0).setPreferredWidth(80);
    table.getColumnModel().getColumn(1).setPreferredWidth(70);
    table.getColumnModel().getColumn(2).setPreferredWidth(400);
   
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    validate();
    }//1024*768

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==button1)
        {
                    if (files==null)
                    {
                      files=new File(path);
                    }
                    String content = "";
                    try{
                        //FileReader one=new FileReader("Memo.BOX");
                        FileReader one=new FileReader("d:\\Calendar_Memo\\Memo.BOX");  //这里应该是指定全部的路径名称
                        
                        char buf[] = new char[1];
                        while(one.read(buf)!=-1)
                        {
                            content+=new String(buf).toString();
                        }
                        
                        String rowMessage[] = content.split(",");    //把日期以 "," 分开来,我把你存的格式改了一下,方便可以用","来分开信息
                        
                        int rowCount = rowMessage.length;
                        
                        for(int i=0;i<(rowCount/3);i++)    //把得到的日志数量除以三就是日志的行数了
                        {
                            for(int j=0;j<3;j++)
                            {
                                a[i][j]=rowMessage[i*3 + j];
                                System.out.println(rowMessage[i*3 + j]);
                            }
                        }
                        table=new JTable(a,name);
                        //validate();  这里不应该是调用validate方法,而是调用重绘的方法
                        repaint();
        ///////////////////////////////////////////////////////////////////////////                
                    }catch(Exception ex)
                    {
                        ex.printStackTrace();
                    }
                    validate();
        }
        if(e.getSource()==item1)
        {
            dispose();
            System.exit(0);
        }
        if(e.getSource()==item4)
        {
            JOptionPane.showMessageDialog(this,"本軟件由XXX熱情製作,專利所有,請勿copy","關於",JOptionPane.INFORMATION_MESSAGE);
        }
        if(e.getSource()==item2)
        {
            dialog2.setVisible(true);
        }
        if(e.getSource()==item3)
        {
            dialog3.setVisible(true);
        }
    }
}

class MyDialog2 extends Dialog implements ActionListener,ItemListener
{
    Panel controltime,controldates,controlnorth,controldatemeno,controltimes,controlpanel;
    Label starttime,years,months,days,符号1,dates,startmeno,yyy,mmm,ddd;
    TextField yearss,monthss,dayss,starthour,starminute,yearssss,monthssss,dayssss;
    JTextArea note;
    Button ok,cancel,choose1,choose2;
    Checkbox longtime;
    Calendar calendar;
    Boolean longyear=false;
    Boolean diaoyong=false;
    File file=null;
    String date;
    String datemeno;
    String writeFile=new String();
    String readFile = new String();
    String memo;
    private static final String path="D:/Calendar_Memo/Memo.BOX";
   
    MyDialog2(JFrame f,String s,boolean b)
    {
        super(f,s,b);
        Toolkit tool=getToolkit();
        Dimension dim=tool.getScreenSize();
        setBounds((dim.width/3)+50,(dim.height/3)+50,410,315);//65
        
        longtime=new Checkbox("年周期节日");
        choose1=new Button("选择");
        choose2=new Button("选择");
        ok=new Button("确定");
        cancel=new Button("取消");
        yearssss=new TextField(10);
        monthssss=new TextField(2);
        dayssss=new TextField(2);
        yearss=new TextField(10);
        monthss=new TextField(2);
        dayss=new TextField(2);
        starthour=new TextField(2);
        starminute=new TextField(2);
        starttime=new Label("发生时间:");
        startmeno=new Label("提醒日期:");
        years=new Label("年");
        months=new Label("月");
        days=new Label("日");
        yyy=new Label("年");
        mmm=new Label("月");
        ddd=new Label("日");
        符号1=new Label(":");
        dates=new Label("日期:");
        note=new JTextArea("请输入要记入的事情",4,20);
        note.setLineWrap(true);
        controlnorth=new Panel();
        controldatemeno=new Panel();
        controlpanel=new Panel();
        controldates=new Panel();
        controltimes=new Panel();
        controltime=new Panel();
        controltime.setLayout(new BorderLayout());
        controlnorth.setLayout(new BorderLayout());
        controlnorth.setBounds(0,0,410,130);
        
        controltimes.add(starttime);
        controltimes.add(starthour);
        controltimes.add(符号1);
        controltimes.add(starminute);
        controltimes.add(longtime);
        
        controldates.add(dates);
        controldates.add(yearss);
        controldates.add(years);
        controldates.add(monthss);
        controldates.add(months);
        controldates.add(dayss);
        controldates.add(days);
        controldates.add(choose1);
        
        controlpanel.add(ok);
        controlpanel.add(cancel);
        
        controldatemeno.add(startmeno);
        controldatemeno.add(yearssss);
        controldatemeno.add(yyy);
        controldatemeno.add(monthssss);
        controldatemeno.add(mmm);
        controldatemeno.add(dayssss);
        controldatemeno.add(ddd);
        controldatemeno.add(choose2);
        
        controlnorth.add(controldates,BorderLayout.NORTH);
        controlnorth.add(controldatemeno,BorderLayout.SOUTH);
        controltime.add(controlnorth,BorderLayout.NORTH);
        controltime.add(controltimes,BorderLayout.SOUTH);
        add(controltime,BorderLayout.NORTH);
        add(note,BorderLayout.CENTER);
        add(controlpanel,BorderLayout.SOUTH);
        ok.addActionListener(this);
        choose2.addActionListener(this);
        choose1.addActionListener(this);
        cancel.addActionListener(this);
        longtime.addItemListener(this);
        
        addWindowListener(new WindowAdapter()
                      {
                          public void windowClosing(WindowEvent e)
                          {
                              dispose();
                          }
                      });
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==choose2)
        {
          calendar=Calendar.getInstance();   
      int y=calendar.get(Calendar.YEAR);           
      int m=calendar.get(Calendar.MONTH)+1;        
      int d=calendar.get(Calendar.DAY_OF_MONTH);
      new CalendarPad(y,m,d,this);
      diaoyong=false;
    }
        if(e.getSource()==choose1)
        {
          calendar=Calendar.getInstance();   
      int y=calendar.get(Calendar.YEAR);           
      int m=calendar.get(Calendar.MONTH)+1;        
      int d=calendar.get(Calendar.DAY_OF_MONTH);
      new CalendarPad(y,m,d,this);
      diaoyong=true;
    }
    else if(e.getSource()==cancel)
    {
        dispose();
    }
    if(e.getSource()==ok)
    {
        try
        {
            int yearsss,mouthsss,daysss,starthours,starminutes;
            yearsss=Integer.parseInt(yearss.getText());
            mouthsss=Integer.parseInt(monthss.getText());
            daysss=Integer.parseInt(dayss.getText());
            starthours=Integer.parseInt(starthour.getText());
            starminutes=Integer.parseInt(starminute.getText());
            StringBuffer str=new StringBuffer(yearss.getText());
            str.append(monthss.getText());
            str.append(starthour.getText());
            str.append(starminute.getText());
            date=new String(str);
            StringBuffer dtr=new StringBuffer(yearssss.getText());
            dtr.append(monthssss.getText());
            dtr.append(dayssss.getText());
            datemeno=new String(dtr);
            memo=note.getText().trim();
            if(memo.length()== 0)
        {
          JOptionPane.showMessageDialog(this,"备忘录内容不能为空");
          return;
        }
            if((yearsss>9999&&mouthsss>12&&mouthsss<1&&daysss>31&&daysss<1&&starthours<1&&starthours>24&&starminutes<1&&starminutes>60)==false)
            {
                try
                {
                  if (file==null)
            {
              file=new File(path);
            }
            BufferedReader bf=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            while((readFile=bf.readLine())!=null)
            {   
              writeFile+=readFile+"\r\n";
            }
            bf.close();
            writeFile+=date+ ","+datemeno+","+memo +",";
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));
            bw.write(writeFile);
            bw.close();
            JOptionPane.showMessageDialog(this, "已保存");
            dispose();
          }
          catch (IOException s)
          {
            System.out.println("------" + s.getMessage() + "------");
          }
        }
        }
        catch(Exception ee)
        {
            note.append("请输入正确的字符");
        }
    }
    }
   
    public void itemStateChanged(ItemEvent e)
    {
        Checkbox box=(Checkbox)e.getSource();
        if(box.getState())
        {
            longyear=true;
        }
    }
    public void 设置日期(int year,int month,int day)
    {
        if(diaoyong==true)
        {
          yearss.setText(""+year);
          monthss.setText(""+month);
          dayss.setText(""+day);
        }
        if(diaoyong==false)
        {
          yearssss.setText(""+year);
          monthssss.setText(""+month);
          dayssss.setText(""+day);
        }
    }
    public void 显示错误()
    {
        yearss.setText("请点击日期");
        monthss.setText("");
        dayss.setText("");
    }
}

class MyDialog3 extends Dialog
{
    Button delete;
    JTable table;
    Object a[][];
    Object name[]={"日期","时间","事件"};
    Panel panel1,panel3;
    Label label1,label2,label3;
   
    MyDialog3(JFrame f,String s,boolean b)
    {
        super(f,s,b);
        Toolkit tool=getToolkit();
        Dimension dim=tool.getScreenSize();
        setBounds((dim.width/3)+50,(dim.height/3)+50,350,250);
        
        a=new Object[8][3];
        for(int i=0;i<8;i++)
        {
            for(int j=0;j<3;j++)
            {
                a[i][j]="0";
            }
        }
        
        table=new JTable(a,name);
        add(table,BorderLayout.CENTER);
        table.getColumnModel().getColumn(0).setPreferredWidth(80);
    table.getColumnModel().getColumn(1).setPreferredWidth(65);
    table.getColumnModel().getColumn(2).setPreferredWidth(205);
   
    label1=new Label("日期",Label.LEFT);
    label2=new Label("时间",Label.LEFT);
    label3=new Label("事情",Label.LEFT);
    label1.setBounds(0,0,80,18);
    label2.setBounds(80,0,65,18);
    label3.setBounds(145,0,205,18);
    panel1=new Panel();
    panel1.setBounds(0,0,350,18);
    panel1.setLayout(null);
    panel3=new Panel();
    delete=new Button("删除");
   
    panel1.add(label1);
    panel1.add(label2);
    panel1.add(label3);
    panel3.add(delete);
   
    add(panel1,BorderLayout.NORTH);
    add(panel3,BorderLayout.SOUTH);
        
        addWindowListener(new WindowAdapter()
                      {
                          public void windowClosing(WindowEvent e)
                          {
                              setVisible(false);
                          }
                      });
    }
}

class CalendarPad extends JFrame implements MouseListener
{
  int year,month,day;                           
  JButton showDay[];            
  JLabel title[];                  
  Calendar 日历;
  int 星期几;         
  Month 负责改变月;
  Year  负责改变年;
  String 星期[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
  JPanel leftPanel;
  MyDialog2 dialog2;
  public  CalendarPad(int year,int month,int day,MyDialog2 dialog2)
  {
       this.dialog2=dialog2;
    leftPanel=new JPanel();
    JPanel leftCenter=new JPanel();
     JPanel leftNorth=new JPanel();
     leftCenter.setLayout(new GridLayout(7,7));   
     this.year=year;
     this.month=month;
     this.day=day;
     负责改变年=new Year(this);
     负责改变年.setYear(year);
     负责改变月=new Month(this);
     负责改变月.setMonth(month);
     title=new JLabel[7];                        
     showDay=new JButton[42];               
     for(int j=0;j<7;j++)                        
       {
         title[j]=new JLabel();
         title[j].setText(星期[j]);
         title[j].setBorder(BorderFactory.createRaisedBevelBorder());
         leftCenter.add(title[j]);
       }
     title[0].setForeground(Color.red);
     title[6].setForeground(Color.blue);

     for(int i=0;i<42;i++)                        
       {
         showDay[i]=new JButton();
         showDay[i].addMouseListener(this);
         leftCenter.add(showDay[i]);
       }   
     日历=Calendar.getInstance();
     Box box=Box.createHorizontalBox();         
     box.add(负责改变年);
     box.add(负责改变月);
     leftNorth.add(box);
     leftPanel.setLayout(new BorderLayout());
     leftPanel.add(leftNorth,BorderLayout.NORTH);
     leftPanel.add(leftCenter,BorderLayout.CENTER);
     leftPanel.add(new Label("请在年份输入框输入所查年份(负数表示公元前),并回车确定"),
                  BorderLayout.SOUTH) ;
     leftPanel.validate();
     add(leftPanel,BorderLayout.CENTER);
     设置日历牌(year,month);
    setVisible(true);
    setBounds(100,50,410,285);
    validate();
   }
  public void 设置日历牌(int year,int month)
   {
     日历.set(year,month-1,1);              
     
     星期几=日历.get(Calendar.DAY_OF_WEEK)-1;
     if(month==1||month==2||month==3||month==5||month==7
                        ||month==8||month==10||month==12)
        {
            排列号码(星期几,31);         
        }
     else if(month==4||month==6||month==9||month==11)
        {
            排列号码(星期几,30);
        }
     else if(month==2)
        {
         if((year%4==0&&year%100!=0)||(year%400==0))  
           {
             排列号码(星期几,29);
           }
         else
           {
             排列号码(星期几,28);
           }
       }
   }
  public void 排列号码(int 星期几,int 月天数)
   {
      for(int i=星期几,n=1;i<星期几+月天数;i++)
             {
                  Calendar calendar=Calendar.getInstance();
               calendar.setTime(new Date());
               int m=calendar.get(Calendar.MONTH)+1;
               showDay[i].setText(""+n);
               if(m==month&&n==day)
               {
                    showDay[i].setForeground(Color.green);
                 showDay[i].setFont(new Font("TimesRoman",Font.BOLD,20));
               }
               else
               {
                 showDay[i].setFont(new Font("TimesRoman",Font.BOLD,12));
                 showDay[i].setForeground(Color.black);
               }
               if(i%7==6)
                 {
                  showDay[i].setForeground(Color.blue);  
                 }
               if(i%7==0)
                 {
                  showDay[i].setForeground(Color.red);  
                 }
               n++;
             }
       for(int i=0;i<星期几;i++)
             {
                showDay[i].setText("");
             }
       for(int i=星期几+月天数;i<42;i++)
             {
                showDay[i].setText("");
             }
   }
public int getYear()
   {
    return year;
   }
public void setYear(int y)
   {
     year=y;
   }
public int getMonth()
   {
    return month;
   }
public void setMonth(int m)
   {
     month=m;
   }
public int getDay()
   {
    return day;
   }
public void mousePressed(MouseEvent e)            
   {
     JButton source=(JButton)e.getSource();
     try{
         day=Integer.parseInt(source.getText());
         dialog2.设置日期(year,month,day);
         setVisible(false);
        }
      catch(Exception ee)
        {
            dialog2.显示错误();
            setVisible(false);
        }
   }
public void mouseClicked(MouseEvent e)
   {
   }
public void mouseReleased(MouseEvent e)
   {
   }
public void mouseEntered(MouseEvent e)
   {
   }
public void mouseExited(MouseEvent e)
   {
   }
}
class Year extends Box implements ActionListener
{
  int year;                           
  JTextField showYear=null;           
  JButton 明年,去年;
  CalendarPad 日历;
  public Year(CalendarPad 日历)
  {  
     super(BoxLayout.X_AXIS);        
     showYear=new JTextField(4);
     showYear.setForeground(Color.blue);
     showYear.setFont(new Font("TimesRomn",Font.BOLD,14));
     this.日历=日历;
     year=日历.getYear();
     明年=new JButton("下年");
     去年=new JButton("上年");
     add(去年);
     add(showYear);
     add(明年);
     showYear.addActionListener(this);
     去年.addActionListener(this);
     明年.addActionListener(this);
  }
public void setYear(int year)
  {
    this.year=year;
    showYear.setText(""+year);
  }
public int getYear()
  {
    return year;
  }
public void actionPerformed(ActionEvent e)
  {
    if(e.getSource()==去年)
      {
        year=year-1;
        showYear.setText(""+year);
        日历.setYear(year);
        日历.设置日历牌(year,日历.getMonth());
      }
    else if(e.getSource()==明年)
      {
        year=year+1;
        showYear.setText(""+year);
        日历.setYear(year);
        日历.设置日历牌(year,日历.getMonth());
      }
    else if(e.getSource()==showYear)
      {
         try
            {
              year=Integer.parseInt(showYear.getText());
              showYear.setText(""+year);
              日历.setYear(year);
              日历.设置日历牌(year,日历.getMonth());
            }
         catch(NumberFormatException ee)
            {
              showYear.setText(""+year);
              日历.setYear(year);
              日历.设置日历牌(year,日历.getMonth());
            }
      }
  }   
}

class Month extends Box implements ActionListener
{
  int month;                           
  JTextField showMonth=null;           
  JButton 下月,上月;
  CalendarPad 日历;
  public Month(CalendarPad 日历)
  {  
     super(BoxLayout.X_AXIS);        
     this.日历=日历;
     showMonth=new JTextField(2);
     month=日历.getMonth();
     showMonth.setEditable(false);
     showMonth.setForeground(Color.blue);
     showMonth.setFont(new Font("TimesRomn",Font.BOLD,16));
     下月=new JButton("下月");
     上月=new JButton("上月");
     add(上月);
     add(showMonth);
     add(下月);
     上月.addActionListener(this);
     下月.addActionListener(this);
     showMonth.setText(""+month);
  }
public void setMonth(int month)
  {
    if(month<=12&&month>=1)
      {
       this.month=month;
      }
    else
      {
        this.month=1;
      }
    showMonth.setText(""+month);
  }
public int getMonth()
  {
    return month;
  }
public void actionPerformed(ActionEvent e)
  {
    if(e.getSource()==上月)
      {
        if(month>=2)
         {
            month=month-1;
            日历.setMonth(month);
            日历.设置日历牌(日历.getYear(),month);
         }
        else if(month==1)
         {
            month=12;
            日历.setMonth(month);
            日历.设置日历牌(日历.getYear(),month);
         }
       showMonth.setText(""+month);
      }
    else if(e.getSource()==下月)
      {
        if(month<12)
          {
            month=month+1;
            日历.setMonth(month);
            日历.设置日历牌(日历.getYear(),month);
          }
        else if(month==12)
          {
            month=1;
            日历.setMonth(month);
            日历.设置日历牌(日历.getYear(),month);
          }
        showMonth.setText(""+month);
      }
  }   
}

public class liangmin
{
    public static void main(String args[])
    {
        liangbing liang=new liangbing("备忘录");
    }
}
2008-06-29 20:04



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-221850-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.220395 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved