java 做日历_Java实现的日历(原创)

import java.awt.*;

import javax.swing.*;

import javax.swing.border.EmptyBorder;

import java.awt.event.*;

import java.util.*;

//写于2011年5月10日,星期二!2011年,5月1日是星期日

public class Calender extends JFrame implements ActionListener,MouseListener

{

private JTextField[] jtf_day;

private JLabel Status;

private String year;

private String month;

private String day;

private JComboBox jcb_year;

private JComboBox jcb_month;

private JButton UpMonthBt;

private JButton NextMonthBt;

public Calender()

{

Date date = new Date();

String[] str_datetime = date.toLocaleString().split(" ");

String[] str_date = str_datetime[0].split("-");

year = str_date[0];

month = str_date[1];

day = str_date[2];

//System.out.println(year+" "+month);

this.setTitle("日历(2011年—2022年)");

this.getContentPane().setLayout(new BorderLayout());

this.setBounds(100,100, 430, 500);

JPanel UpPanel = new JPanel();

UpPanel.setLayout(new FlowLayout());

JLabel labeldata = new JLabel("日期:");

UpPanel.add(labeldata);

String[] YearData = {"2011","2012","2013","2014","2015","2016","2017","2018","2019","2020","2021","2022"};

jcb_year = new JComboBox();

for(String str:YearData)

{

jcb_year.addItem(str);

}

jcb_year.setSelectedItem(year);

jcb_year.addActionListener(this);

UpPanel.add(jcb_year);

JLabel labelyear = new JLabel("年");

UpPanel.add(labelyear);

String[] MonthData = {"1","2","3","4","5","6","7","8","9","10","11","12"};

jcb_month = new JComboBox();

for(String str:MonthData)

{

jcb_month.addItem(str);

}

jcb_month.setSelectedItem(month);

jcb_month.addActionListener(this);

UpPanel.add(jcb_month);

JLabel labelmonth = new JLabel("月");

UpPanel.add(labelmonth);

UpMonthBt = new JButton("上月");

UpMonthBt.addActionListener(this);

UpPanel.add(UpMonthBt);

NextMonthBt = new JButton("下月");

NextMonthBt.addActionListener(this);

UpPanel.add(NextMonthBt);

this.getContentPane().add(UpPanel,BorderLayout.NORTH);

JPanel CenterPanel = new JPanel();

CenterPanel.setLayout(new GridLayout(7,7));

JTextField[] jtf_week =new JTextField[7];

jtf_week[0] = new JTextField("日");

jtf_week[1] = new JTextField("一");

jtf_week[2] = new JTextField("二");

jtf_week[3] = new JTextField("三");

jtf_week[4] = new JTextField("四");

jtf_week[5] = new JTextField("五");

jtf_week[6] = new JTextField("六");

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

{

jtf_week[i].setHorizontalAlignment(JTextField.CENTER);

jtf_week[i].setEditable(false);

//jtf_week[i].addActionListener(this);

//jtf_week[i].addMouseListener(this);

jtf_week[i].setBackground(Color.gray);

CenterPanel.add(jtf_week[i]);

}

jtf_day = new JTextField[42];

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

{

jtf_day[i]= new JTextField();

jtf_day[i].setEditable(false);

jtf_day[i].setBackground(Color.WHITE);

jtf_day[i].addMouseListener(this);

CenterPanel.add(jtf_day[i]);

}

this.getContentPane().add(CenterPanel,BorderLayout.CENTER);

JPanel DownPanel = new JPanel();

Status = new JLabel("日期:"+year+"年"+month+"月"+day+"日");

DownPanel.add(Status);

this.getContentPane().add(DownPanel,BorderLayout.SOUTH);

ChangeJtf(year,month);

}

private int CacalateDay(int yeartemp,int montemp)

{

int day=0;

int step=-1;

int[] mon = new int[12];

if(JudgeYear(yeartemp))

{

mon[0]=31;mon[1]=29;mon[2]=31;mon[3]=30;mon[4]=31;mon[5]=30;

mon[6]=31;mon[7]=31;mon[8]=30;mon[9]=31;mon[10]=30;mon[11]=31;

}else

{

mon[0]=31;mon[1]=28;mon[2]=31;mon[3]=30;mon[4]=31;mon[5]=30;

mon[6]=31;mon[7]=31;mon[8]=30;mon[9]=31;mon[10]=30;mon[11]=31;

}

if(yeartemp==2011&&montemp==1)

{

return 6;

}else

{

int startyear = 2011;

int startmonth = 1;

while(yeartemp!=startyear||montemp!=startmonth)

{

day=day+mon[startmonth-1];

startmonth+=1;

if(startmonth==13)

{

startmonth=1;

startyear++;

if(JudgeYear(startyear))

{

mon[0]=31;mon[1]=29;mon[2]=31;mon[3]=30;mon[4]=31;mon[5]=30;

mon[6]=31;mon[7]=31;mon[8]=30;mon[9]=31;mon[10]=30;mon[11]=31;

}else

{

mon[0]=31;mon[1]=28;mon[2]=31;mon[3]=30;mon[4]=31;mon[5]=30;

mon[6]=31;mon[7]=31;mon[8]=30;mon[9]=31;mon[10]=30;mon[11]=31;

}

}

}

}

return day-1;

}

private void ChangeJtf(String y,String m)

{

int yeartemp = Integer.parseInt(y);

int montemp = Integer.parseInt(m);

if(yeartemp==2011&&montemp==1)

{

UpMonthBt.setEnabled(false);

}

if(yeartemp==2022&&montemp==12)

{

NextMonthBt.setEnabled(false);

}

int day = CacalateDay(yeartemp,montemp);

int[] mon = new int[12];

if(JudgeYear(yeartemp))

{

mon[0]=31;mon[1]=29;mon[2]=31;mon[3]=30;mon[4]=31;mon[5]=30;

mon[6]=31;mon[7]=31;mon[8]=30;mon[9]=31;mon[10]=30;mon[11]=31;

}else

{

mon[0]=31;mon[1]=28;mon[2]=31;mon[3]=30;mon[4]=31;mon[5]=30;

mon[6]=31;mon[7]=31;mon[8]=30;mon[9]=31;mon[10]=30;mon[11]=31;

}

for(int i=0;i < day%7;i++)

{

//jtf_day[i].setBackground(Color.lightGray);

jtf_day[i].setText("");

}

for(int i = day%7,j=1;i < mon[montemp-1]+day%7;i++,j++)

{

jtf_day[i].setText(j+"");

//jtf_day[i].setBackground(Color.WHITE);

jtf_day[i].setHorizontalAlignment(JLabel.CENTER);

}

for(int i= mon[montemp-1]+day%7;i<42;i++)

{

//jtf_day[i].setBackground(Color.lightGray);

jtf_day[i].setText("");

}

}

private void BtactionPerformed(ActionEvent e)

{

JButton bt = (JButton)e.getSource();

int montemp = Integer.parseInt(month);

int yeartemp = Integer.parseInt(year);

if(bt.getText()=="上月")

{

if(montemp==1)

{

yeartemp--;

montemp=12;

year = ""+yeartemp;

jcb_year.setSelectedIndex(yeartemp-2011);

}

else

{

montemp--;

}

month = ""+montemp;

jcb_month.setSelectedIndex(montemp-1);

}

if(bt.getText()=="下月")

{

if(montemp==12)

{

montemp=1;

yeartemp++;

year = ""+yeartemp;

jcb_year.setSelectedIndex(yeartemp-2011);

}

else

{

montemp++;

}

month = ""+montemp;

jcb_month.setSelectedIndex(montemp-1);

}

}

private void JcbactionPerformed(ActionEvent e)

{

int y = 2011+jcb_year.getSelectedIndex();

int m = 1+jcb_month.getSelectedIndex();

year = y+"";

month = m+"";

}

public void actionPerformed(ActionEvent e)

{

if((e.getSource()) instanceof JButton)

{

BtactionPerformed(e);

}else if((e.getSource()) instanceof JComboBox)

{

JcbactionPerformed(e);

}

ChangeJtf(year,month);

}

private boolean JudgeYear(int year)

{

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

{

return true;

}

else return false;

}

public void mousePressed(MouseEvent e){}

public void mouseReleased(MouseEvent e){}

public void mouseClicked(MouseEvent e){}

public void mouseEntered(MouseEvent e)

{

JTextField jtf = (JTextField)(e.getSource());

if(!jtf.getText().equals(""))

{

jtf.setBackground(Color.BLUE);

String date = "日期:"+year+"年"+month+"月"+jtf.getText()+"日";

Status.setText(date);

}

//System.out.println(jtf.getText());

//System.out.println("flag");

}

public void mouseExited(MouseEvent e)

{

JTextField jtf = (JTextField)(e.getSource());

if(!jtf.getText().equals(""))

{

jtf.setBackground(Color.WHITE);

}

}

public static void main(String[] args) {

Calender a = new Calender();

a.setVisible(true);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值