博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java实现的日历(原创)
阅读量:4943 次
发布时间:2019-06-11

本文共 6685 字,大约阅读时间需要 22 分钟。

源代码:

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); } }

转载于:https://www.cnblogs.com/JPAORM/archive/2011/05/12/2510103.html

你可能感兴趣的文章
percent的用法
查看>>
Hibernate三种状态详解
查看>>
判断一个数是否是2^N次方
查看>>
js中几种实用的跨域方法原理详解
查看>>
打印图形
查看>>
《第一行代码》学习笔记7-活动Activity(5)
查看>>
ngx_http_core_module 模块
查看>>
两个常见的oracle索引
查看>>
一位有着工匠精神的博主写的关于IEnumerable接口的详细解析
查看>>
MySQL中特有的函数If函数
查看>>
安装Python3.6.2报错:zipimport.ZipImportError: can't decompress data; zlib not available
查看>>
【蓝桥杯】入门训练 Fibonacci数列
查看>>
实验十 指针2
查看>>
常见HTTP状态码
查看>>
vim 空格和换行的删除和替换
查看>>
ionic 入门学习
查看>>
[python]pickle和cPickle
查看>>
末日了,天是灰色的。
查看>>
Vuejs vm对象详解
查看>>
自定义RatingBar的一个问题(只显示显示一个星星)
查看>>