//Calendar日历记事本
package wannianli;
import java.util.Calendar;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;//文件访问(Java IO)粗体+ 波浪线
import java.util.Hashtable;//哈希表 粗下划线
public class CalendarPad extends JFrame implements MouseListener{
int year,month,day;
Hashtable hashtable;
File file;
JTextField showDay[];
JLabel title[];
JLabel[] label=new JLabel[49];
JLabel y_label=new JLabel(Messages.getString("CalendarPad.0")); //$NON-NLS-1$
JLabel m_label=new JLabel(Messages.getString("CalendarPad.1")); //$NON-NLS-1$
Calendar cal;
Calendar now=Calendar.getInstance();// 实例化Calendar
int week;
NotePad notepad=null;
Month ChangeMonth;
Year ChangeYear;
String w[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$
JPanel leftPanel,rightPanel;
public CalendarPad(int year,int month,int day)//构造函数
{
leftPanel=new JPanel();
JPanel leftCenter=new JPanel();
JPanel leftNorth=new JPanel();
leftCenter.setLayout(new GridLayout(7,7));
rightPanel=new JPanel();
this.year=year;
this.month=month;
this.day=day;
ChangeYear=new Year(this);
ChangeYear.setYear(year);
ChangeMonth=new Month(this);
ChangeMonth.setMonth(month);
title=new JLabel[7];
showDay=new JTextField[42];
for(int j=0;j<7;j++){
title[j]=new JLabel();
title[j].setText(w[j]);
title[j].setBorder(BorderFactory.createRaisedBevelBorder());//调节星期行的//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 JTextField();
showDay[i].addMouseListener(this);//设置鼠标监听
showDay[i].setEditable(false);//无法直接修改日期的东西
leftCenter.add(showDay[i]);
}
cal=Calendar.getInstance();
Box box=Box.createHorizontalBox();
box.add(ChangeYear);//设置上年,下年
box.add(ChangeMonth);//设置上月,下月
leftNorth.add(box);
leftPanel.setLayout(new BorderLayout());
leftPanel.add(leftNorth,BorderLayout.NORTH);
leftPanel.add(leftCenter,BorderLayout.CENTER);
rightPanel.add(new Label(Messages.getString("年份框输入年份(负数公元前)回车确定")),BorderLayout.SOUTH); //设置年份框输入年份
leftPanel.add(new Clock(this),BorderLayout.SOUTH);//调用Clock 类实例, 来实现对当前时间的显示
leftPanel.validate();
Container con=getContentPane();
JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftPanel,rightPanel);
con.add(split,BorderLayout.CENTER);
con.validate();
hashtable=new Hashtable();
file=new File(Messages.getString("日历记事本 .txt")); //日历记事本
if(!file.exists()){
try{
FileOutputStream out=new FileOutputStream(file);//文件输出流
ObjectOutputStream objectOut=new ObjectOutputStream(out);//Java 对象的基本数据类型和图形写入 OutputStream
objectOut.writeObject(hashtable);
objectOut.close();
out.close();
}catch(IOException e){}
}
notepad=new NotePad(this);
rightPanel.add(notepad);//记事本
setCal(year,month);//日期
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setVisible(true);
setBounds(100,50,524,285);
validate();
}
public void setCal(int year,int month){
cal.set(year,month-1,1);
week=cal.get(Calendar.DAY_OF_WEEK)-1;
System.out.println(week);
if(month==1||month==2||month==3||month==5||month==7||month==8||month==10||month==12){
QueryNum(week,31);
}else if(month==4||month==6||month==9||month==11){
QueryNum(week,30);
}else if(month==2){
if((year%4==0&&year%100!=0)||(year%400==0)){
QueryNum(week,29);
}else{
QueryNum(week,28);
}
}
}
public void QueryNum(int w_num,int month_num){
for(int i=w_num,n=1;i<w_num+month_num;i++){
showDay[i].setText(""+n); //日期
if(n==day){
showDay[i].setForeground(Color.green);
showDay[i].setFont(new Font(Messages.getString("CalendarPad.12"),Font.BOLD,20)); //$NON-NLS-1$
}else{
showDay[i].setFont(new Font(Messages.getString("CalendarPad.13"),Font.BOLD,12)); //$NON-NLS-1$
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<w_num;i++)
{
showDay[i].setText(""); //$NON-NLS-1$
}
for(int i=w_num+month_num;i<42;i++)
{
showDay[i].setText(""); //$NON-NLS-1$
}
}
public int getYear(){return year;}
public void setYear(int y){
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.setMesLabel(ye
notepad.setText(null);
notepad.getLog(year,month,day);
}catch(Exception ee){}
}
public void mouseClicked(MouseEvent e){
//rightPanel.add(notepad);
}
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);
}
}
sunbohan00
相关分类