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             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             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             button_day[i].setText(Integer.toString(++ldays));
            button_day[i].setForeground(Color.GRAY);
        }
        
        int count = 1;
        for(int i = 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             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);
        }
    }
}
 

文章知识点与官方知识档案匹配,可进一步学习相关知识Java技能树首页概览92536 人正在系统学习中

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年5月14日
下一篇 2022年5月14日

相关推荐