Swing 例子:日历

Year.java

 


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public 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());
            }
        }
    }
}

Month.java

 


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public 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("TimesRoman",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);
        }
    }
}

CalendarPad.java

 


import java.util.Calendar;//util包,Calendar类
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Hashtable;//util包,哈希表?可以设置键key,寻找匹配。

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class CalendarPad extends JFrame implements MouseListener//声明接口
{
    int year,month,day;
    Hashtable hashtable;
    File file;
    JTextField showDay[];
    JLabel title[];
    Calendar 日历;//此类有什么方法?
    int 星期几;
    NotePad notepad=null;
    Month 负责改变月;
    Year 负责改变年;
    String 星期[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
    JPanel leftPanel,rightPanel;
    public CalendarPad(int year,int month,int day)
    {
        JPanel leftPanel = new JPanel();
        JPanel leftCenter=new JPanel();
        JPanel leftNorth=new JPanel();
        leftCenter.setLayout(new GridLayout(7,7));
        
        JPanel rightPanel=new JPanel();
        rightPanel.setPreferredSize(new java.awt.Dimension(262, 256));//设置右边面版的大小?
        this.year=year;//把形参传给实参。
        this.month=month;
        this.day=day;
        负责改变年=new Year(this);
        负责改变年.setYear(year);
        负责改变月=new Month(this);
        负责改变月.setMonth(month);
        
        title=new JLabel[7];//用来显示星期的Label
        showDay=new JTextField[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++)//添加显示日期的TextField,并未设置setText()
        {
            showDay[i]=new JTextField();
            showDay[i].addMouseListener(this);
            showDay[i].setEditable(false);
            leftCenter.add(showDay[i]);
        }
        日历=Calendar.getInstance();//得到当前日期?
        Box box=Box.createHorizontalBox();//Box的定义,new 呢 ?
        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();//显示所包含组件
        
        Container con=getContentPane();//什么意义?
        this.setFocusCycleRoot(false);//什么意义?
        JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftPanel,rightPanel);//什么?中间带条的?
        con.add(split,BorderLayout.CENTER);
        con.validate();
        hashtable=new Hashtable();
        file=new File("日历记事本.txt");//在当前目录下新建:日历记事本.txt文件。若已经存在呢?
        if(!file.exists())
        {
            try//文件输入输出操作步骤
            {   //file=new File("日历记事本.txt");建立文件对象
                FileOutputStream out=new FileOutputStream(file);//建立输出流对象,以文件对象为参数。
                ObjectOutputStream objectOut=new ObjectOutputStream(out);//建立输出对象,以输出流为参数。
                objectOut.writeObject(hashtable);//把文件内容写进哈希表。
                objectOut.close();//关闭
                out.close();
            }catch(IOException e)
            {
                
            }
        }
        notepad=new NotePad(this);
        rightPanel.add(notepad);
        设置日历牌(year,month);
        addWindowListener(new WindowAdapter()//覆盖接口方法。
                {
                    public void windowClosing(WindowEvent e)
                    {
                        System.exit(0);
                    }
                });
        setVisible(true);//没有显式声明Frame,即JFrame f=new JFrame("标题");因为继承。
        this.setBounds(29, 52, 570, 285);//大小位置。x,y,Width,Height
        this.setTitle("/u5927/u6c14/u8c61&/u78a7/u5929/u98de/u9e64");
        this.setResizable(false);//窗口大小不可改变。
        validate();//检查包含的组件。
    }
    public void 设置日历牌(int year,int month)
    {
        日历.set(year,month-1,1);//设置哪年哪月哪日,month为什么-1?
        星期几=日历.get(Calendar.DAY_OF_WEEK)-1;//get方法得到上面设置的,为什么-1?
        if(month==1||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++)
        {
            showDay[i].setText(""+n);//""+n表示:把n用字符串表示。
            if(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()//引用时,int a = this.getYear();
    {
        return year;
    }
    public void setYear(int y)//调用时,this.setYear(8);
    {
        year=y;
        notepad.setYear(year);
    }
    public int getMonth()
    {
        return month;
    }
    public void setMonth(int m)
    {
        month=m;
        notepad.setMonth(month);
    }
    public int getDay()
    {
        return day;
    }
    public void setDay(int d)
    {
        day=d;
        notepad.setDay(day);
    }
    public Hashtable getHashtable()
    {
        return hashtable;
    }
    public File getFile()
    {
        return file;
    }
    public void mousePressed(MouseEvent e)
    {
        JTextField source=(JTextField)e.getSource();//定义组件的另外一种方法。此法完全引用了事件源组件的所有属性。
        try
        {
            day=Integer.parseInt(source.getText());
            notepad.setDay(day);
            notepad.设置信息条(year,month,day);
            notepad.设置文本区(null);
            notepad.获取日志内容(year,month,day);
        }catch(Exception ee)
        {
            
        }
    }
    public void mouseClicked(MouseEvent e)
    {
        
    }
    public void mouseReleased(MouseEvent e)
    {
        
    }
    public void mouseEntered(MouseEvent e)
    {
        
    }
    public void mouseExited(MouseEvent e)
    {
        
    }
    public static void main(String args[])
    {
        Calendar 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);
    }
    
}

NotePad.java

 


import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
public class NotePad extends JPanel implements ActionListener
{
    JTextArea text;
    JButton 保存日志,删除日志;
    Hashtable table;
    JLabel 信息条;
    int year,month,day;
    File file;
    CalendarPad calendar;
    public NotePad(CalendarPad calendar)
    {
        this.calendar = calendar;
        year = calendar.getYear();
        month = calendar.getMonth();
        day = calendar.getDay();
        table = calendar.getHashtable();
        file = calendar.getFile();
        信息条=new JLabel(""+year+"年"+month+"月"+day+"日",JLabel.CENTER);
        信息条.setFont(new Font("TimesRoman",Font.BOLD,16));
        信息条.setForeground(Color.blue);
        text = new JTextArea(10,10);
        保存日志=new JButton("保存日志");
        删除日志=new JButton("删除日志");
        保存日志.addActionListener(this);
        删除日志.addActionListener(this);
        setLayout(new BorderLayout());
        JPanel pSouth = new JPanel();
        add(信息条,BorderLayout.NORTH);
        pSouth.add(保存日志);
        pSouth.add(删除日志);
        add(pSouth,BorderLayout.SOUTH);
        add(new JScrollPane(text),BorderLayout.CENTER);
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==保存日志)
        {
            保存日志(year,month,day);
        }
        else if(e.getSource()==删除日志)
        {
            删除日志(year,month,day);
        }
    }
    public void setYear(int year)
    {
        this.year = year;
    }
    public int getYear()
    {
        return year;
    }
    public void setMonth(int month)
    {
        this.month=month;
    }
    public int getMonth()
    {
        return month;
    }
    public void setDay(int day)
    {
        this.day=day;
    }
    public int getDay()
    {
        return day;
    }
    public void 设置信息条(int year,int month,int day)
    {
        信息条.setText(""+year+"年"+month+"月"+day+"日");
    }
    public void 设置文本区(String s)
    {
        text.setText(s);
    }
    public void 获取日志内容(int year,int month,int day)
    {
        String key=""+year+""+month+""+day;
    try
    {
        FileInputStream inOne=new FileInputStream(file);
        ObjectInputStream inTwo=new ObjectInputStream(inOne);
        table=(Hashtable)inTwo.readObject();
        inOne.close();
        inTwo.close();
    }
    catch(Exception ee)
    {
        
    }
    if(table.containsKey(key))
    {
        String m=""+year+"年"+month+"月"+day+"日这一天有日志记录,想看吗?";
        int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
        if(ok==JOptionPane.YES_OPTION)
        {
            text.setText((String)table.get(key));
        }
        else
        {
            text.setText("");
        }
    }
    else
    {
        text.setText("无日志记录");
    }
}
public void 保存日志(int year,int month,int day)
{
    String 日志内容=text.getText();
    String key=""+year+""+month+""+day;
    String m=""+year+"年"+month+"月"+day+"日,保存日志吗?";
    int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
    if(ok==JOptionPane.YES_OPTION)
    {
        try
        {
            FileInputStream inOne=new FileInputStream(file);
            ObjectInputStream inTwo=new ObjectInputStream(inOne);
            table=(Hashtable)inTwo.readObject();
            inOne.close();
            inTwo.close();
            table.put(key,日志内容);
            FileOutputStream out=new FileOutputStream(file);
            ObjectOutputStream objectOut=new ObjectOutputStream(out);
            objectOut.writeObject(table);
            objectOut.close();
            out.close();
        }
        catch(Exception ee)
        {
            
        }
    }
}
public void 删除日志(int year,int month,int day)
{
    String key=""+year+""+month+""+day;
    if(table.containsKey(key))
    {
        String m=""+year+"年"+month+"月"+day+"日的日志吗?";
        int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
        if(ok==JOptionPane.YES_OPTION)
        {
            try
            {
                FileInputStream inOne=new FileInputStream(file);
                ObjectInputStream inTwo=new ObjectInputStream(inOne);
                table=(Hashtable)inTwo.readObject();
                inOne.close();
                inTwo.close();
                table.remove(key);
                FileOutputStream out=new FileOutputStream(file);
                ObjectOutputStream objectOut=new ObjectOutputStream(out);
                objectOut.close();
                out.close();
                text.setText(null);
            }catch(Exception ee)
            {
                
            }
        }
    }
    else
    {
        String m=""+year+"年"+month+"月"+day+"日无日志记录";
        JOptionPane.showMessageDialog(this,m,"提示",JOptionPane.WARNING_MESSAGE);
    }
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值