java实现日历软件

package shixun;

import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Timer;
import java.awt.Color;
import javax.swing.*;

public class CalendarDemo extends JFrame implements ActionListener{

    JTextField jt1 = new JTextField("",10);
    JTextField jt2 = new JTextField("",10);
    JButton btn1 = new JButton("确定");
    JButton btn2 = new JButton("上月");
    JButton btn3 = new JButton("下月");
    
    
    //利用数组进行循环放置按钮
    String[] week = {"日", "一", "二", "三", "四", "五", "六"};
    JButton[] button_week = new JButton[7];
    JButton[] button_day = new JButton[42];
    JLabel D = new JLabel();
    JLabel DATE = new JLabel();
    
    
    //创建框架
    JFrame jf = new JFrame("月历");
    Container c = jf.getContentPane();
    Calendar ca = Calendar.getInstance();
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
    Date today=new Date();
    
    
    public CalendarDemo() {
        
        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);
        
        //设置框架大小
            jf.setSize(700,500);
        //显示框架
            jf.setVisible(true);
        //关闭窗口
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置窗口显示位置
            jf.setLocationRelativeTo(null);
            
            c.setLayout(new FlowLayout());
            c.add(new JLabel("请输入日期"));
            c.add(new JLabel("年份"));
            c.add(jt1);
            c.add(new JLabel("月份"));
            c.add(jt2);
            c.add(btn1);
            c.add(btn2);
            c.add(btn3);
            DATE.setText((today.getYear()+1900) + "年" + (today.getMonth() + 1) + "月");
            c.add(DATE);
            DATE.setForeground(Color.BLACK);
            DATE.setFont(new Font("宋体",Font.BOLD,20));
            D.setText(df.format(new Date()));
        
            JPanel p = new JPanel();
            c.add(p);
            p.setLayout(new GridLayout(7, 7, 5, 5));
            
            //遍历数组,放置按钮
            for(int i = 0; i < 7; i++){
            button_week[i] = new JButton(week[i]);
            button_week[i].setForeground(Color.black);
            p.add(button_week[i]);
            button_week[i].setPreferredSize(new Dimension(90,50));
            }
            
            for(int i = 0; i < 42; i++){
            button_day[i] = new JButton(" ");
            p.add(button_day[i]);
            }
            Time(today.getYear()+1900,today.getMonth()+1);//让文本框的默认日期为今日
            c.add(D);
            
    }


    //获得日历
    public void Time(int year, int month) {
        
        int weekDay = GetFirstDayOfMonth(year,month); //本月开始第一天
        int days = GetDayOfMonth(year,month);   //这个月的天数
        int ldays = GetDayOfMonth(year,month-1) - weekDay;  //上个月在框架中显示的时间  month-1即为上个月的天数
        ca.set(year,month-1,1);
        DATE.setText(year + "年" + month + "月");  
        //修改日期
        for(int i = 0;i < weekDay;i++){                       //上个月
            button_day[i].setText(Integer.toString(++ldays));
            button_day[i].setForeground(Color.GRAY);
        }
        
        int count = 1;
        for(int i = weekDay;i < days+ weekDay;i++){  
            //这个月
            button_day[i].setText(Integer.toString(count++));
            button_day[i].setForeground(Color.black);
            if(i % 7 == 0 || i == 6 || i == 13 || i == 20 || i == 27 || i ==34)
                button_day[i].setForeground(Color.red);
        }
        
        count = 1;
        for(int i = days+weekDay;i < 42;i++){                 //下个月
            button_day[i].setText(Integer.toString(count++));
            button_day[i].setForeground(Color.gray);
        }
    }
    
    public int GetDayOfMonth(int year, int month) {
        Calendar ca = Calendar.getInstance();
        ca.set(year,month-1,1);
        return ca.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    public int GetFirstDayOfMonth(int year, int month) {
        Calendar ca = Calendar.getInstance();
        ca.set(year,month-1,1);
        return ca.get(Calendar.DAY_OF_WEEK)-1;
    }

    public static void main(String[] args) {
        new CalendarDemo();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        
        if(e.getSource() == btn1) {
            try {
                    Time(Integer.parseInt(jt1.getText()),Integer.parseInt(jt2.getText()));
            } catch (Exception e2) {
                JOptionPane.showMessageDialog(null, "输入错误请重新输入!");
            }
            
        }else if (e.getActionCommand().equals("上月")) {
            String[] str = DATE.getText().split("年|月"); //将字符串分给成字符串数组
            if (str[1].equals("1")) 
                Time(Integer.parseInt(str[0])-1,12); //将字符串转换为数字
            else
                Time(Integer.parseInt(str[0]),Integer.parseInt(str[1])-1);
        } else if (e.getActionCommand().equals("下月")) {
            String[] str = DATE.getText().split("年|月");
            if (str[1].equals("12"))
                Time(Integer.parseInt(str[0])+1,1);
            else
                Time(Integer.parseInt(str[0]),Integer.parseInt(str[1])+1);
        }
    }
}
 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值