[Java] 绘制日历 →→→→→进入此内容的聊天室

来自 , 2021-01-17, 写在 Java, 查看 157 次.
URL http://www.code666.cn/view/dc5c768b
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.GridLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.WindowAdapter;
  8. import java.awt.event.WindowEvent;
  9. import java.util.Date;
  10. import java.util.GregorianCalendar;
  11.  
  12. import javax.swing.JButton;
  13. import javax.swing.JComboBox;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JPanel;
  17.  
  18. /**
  19.  * 日历程序
  20.  */
  21. public class CalenderTrain extends JFrame implements ActionListener {
  22.     // 月份和年份的下拉列表框
  23.     private JComboBox MonthBox = new JComboBox();
  24.     private JComboBox YearBox = new JComboBox();
  25.     // 月份和年份的标签
  26.     private JLabel YearLabel = new JLabel("年份: ");
  27.     private JLabel MonthLabel = new JLabel("月份: ");
  28.     // “确定”和“今天”两个按钮
  29.     private JButton button_ok = new JButton("查 看");
  30.     private JButton button_today = new JButton("今 天");
  31.  
  32.     // 获取今天的日期、年份、月份
  33.     private Date now_date = new Date();
  34.     private int now_year = now_date.getYear() + 1900;
  35.     private int now_month = now_date.getMonth();
  36.     // 用否显示今天的日期
  37.     private boolean todayFlag = false;
  38.  
  39.     // 用一组按钮显示日期,一共7行7列,第一行为星期的名字
  40.     private JButton[] button_day = new JButton[42];
  41.     private final String[] week = { "日", "一", "二", "三", "四", "五", "六" };
  42.     private JButton[] button_week = new JButton[7];
  43.     // 保存用户选择的年份
  44.     private String year_int = null;
  45.     // 保存用户选择的月份
  46.     private int month_int;
  47.  
  48.     /**
  49.      * 构造函数
  50.      */
  51.     public CalenderTrain() {
  52.         super();
  53.         // 设置标题
  54.         this.setTitle("日 历");
  55.         this.init();
  56.         this.setLocation(500, 300);
  57.         // 面板的大小不能变化
  58.         this.setResizable(false);
  59.         pack();
  60.     }
  61.  
  62.     /**
  63.      * 初始化日历
  64.      */
  65.     private void init() {
  66.         Font font = new Font("Dialog", Font.BOLD, 14);
  67.         YearLabel.setFont(font);
  68.         MonthLabel.setFont(font);
  69.         button_ok.setFont(font);
  70.         button_today.setFont(font);
  71.         // 设定年份区间,为当前年份的过去10年到当前年份的未来20年,
  72.         for (int i = now_year - 10; i <= now_year + 20; i++) {
  73.             YearBox.addItem(i + "");
  74.         }
  75.         // 设定年份下拉列表为当前年份,当前月份处于第十项
  76.         YearBox.setSelectedIndex(10);
  77.         // 设定月份区间,12个月
  78.         for (int i = 1; i < 13; i++) {
  79.             MonthBox.addItem(i + "");
  80.         }
  81.         // 设定月份下拉列表为当前月份
  82.         MonthBox.setSelectedIndex(now_month);
  83.  
  84.         // 放置下拉列表框和控制按钮的面板
  85.         JPanel panel_ym = new JPanel();
  86.         panel_ym.add(YearLabel);
  87.         panel_ym.add(YearBox);
  88.         panel_ym.add(MonthLabel);
  89.         panel_ym.add(MonthBox);
  90.         panel_ym.add(button_ok);
  91.         panel_ym.add(button_today);
  92.         // 为2个按钮添加事件侦听器
  93.         button_ok.addActionListener(this);
  94.         button_today.addActionListener(this);
  95.  
  96.         // 放置日期面板
  97.         JPanel panel_day = new JPanel();
  98.         // 网格布局管理器,7行7列,网格之间水平和垂直方向上间隔均为5
  99.         panel_day.setLayout(new GridLayout(7, 7, 3, 3));
  100.         // 添加星期的名字,并放到画板里
  101.         for (int i = 0; i < 7; i++) {
  102.             button_week[i] = new JButton(" ");
  103.             button_week[i].setText(week[i]);
  104.             button_week[0].setForeground(Color.black);
  105.             panel_day.add(button_week[i]);
  106.         }
  107.         button_week[0].setForeground(Color.red);
  108.         button_week[6].setForeground(Color.red);
  109.         // 添加日期,放到画板里
  110.         for (int i = 0; i < 42; i++) {
  111.             button_day[i] = new JButton(" ");
  112.             panel_day.add(button_day[i]);
  113.         }
  114.         // 显示当前年月的日期
  115.         this.paintDay();
  116.  
  117.         // 放置以上两个面板
  118.         JPanel panel_main = new JPanel();
  119.         // 边界布局管理器
  120.         panel_main.setLayout(new BorderLayout());
  121.         panel_main.add(panel_day, BorderLayout.SOUTH);
  122.         panel_main.add(panel_ym, BorderLayout.NORTH);
  123.         getContentPane().add(panel_main);
  124.     }
  125.  
  126.     /**
  127.      * 显示当前年月的日期
  128.      */
  129.     private void paintDay() {
  130.         if (todayFlag) {
  131.             // 要求显示今天的日历
  132.             year_int = now_year + "";
  133.             month_int = now_month;
  134.         } else {
  135.             // 否则,从下拉框中获取用户选择的年月
  136.             year_int = YearBox.getSelectedItem().toString();
  137.             month_int = MonthBox.getSelectedIndex();// 被选的序号
  138.         }
  139.         // 获得年份值
  140.         int year_sel = Integer.parseInt(year_int) - 1900;
  141.         // 构造该月的第一天
  142.         Date firstDay = new Date(year_sel, month_int, 1);
  143.         // 创建一个Calendar实例
  144.         GregorianCalendar cal = new GregorianCalendar();
  145.         cal.setTime(firstDay);
  146.         int days = 0; // 存放某个月份的天数
  147.         int day_week = 0; // 存放某个月的第一天是星期几的数值
  148.  
  149.         // 判断是几月份,根据它来设定day的值 其中二月份要判断是否是闰年
  150.         if (month_int == 0 || month_int == 2 || month_int == 4
  151.                 || month_int == 6 || month_int == 7 || month_int == 9
  152.                 || month_int == 11) {
  153.             days = 31;
  154.         } else if (month_int == 3 || month_int == 5 || month_int == 8
  155.                 || month_int == 10) {
  156.             days = 30;
  157.         } else {
  158.             // 二月,如果是闰年,则有29天,否则有28天
  159.             if (cal.isLeapYear(year_sel)) {
  160.                 days = 29;
  161.             } else {
  162.                 days = 28;
  163.             }
  164.         }
  165.         // getDay方法获取该天是星期几
  166.         day_week = firstDay.getDay();
  167.         int count = 1;
  168.         /*
  169.          * 绘制按钮。在这里首先要根据选定的月份的第一天是星期几来确定绘制按钮的起始位置
  170.          * 其中day_week就是我们要绘制的起始位置,对于那些没有数值可以显示的按钮要置空。
  171.          */
  172.         for (int i = day_week; i < day_week + days; count++, i++) {
  173.             if (i % 7 == 0 || i == 6 || i == 13 || i == 20 || i == 27
  174.                     || i == 34 || i == 41) {
  175.                 // 如果是边界上的按钮,文字用红色,以标示周末
  176.                 if (i == day_week + now_date.getDate() - 1) {
  177.                     // 将跟今天一样的日期用蓝色标示
  178.                     button_day[i].setForeground(Color.blue);
  179.                     button_day[i].setText(count + "");
  180.                 } else {
  181.                     // 其他边界上的按钮中的文字用红色
  182.                     button_day[i].setForeground(Color.red);
  183.                     button_day[i].setText(count + "");
  184.                 }
  185.  
  186.             } else {
  187.                 if (i == day_week + now_date.getDate() - 1) {
  188.                     button_day[i].setForeground(Color.blue);
  189.                     button_day[i].setText(count + "");
  190.                 } else {
  191.                     // 一般位置的按钮上的文字用黑色标示
  192.                     button_day[i].setForeground(Color.black);
  193.                     button_day[i].setText(count + "");
  194.                 }
  195.             }
  196.         }
  197.         // 对于没有日期数值显示的按钮进行置空处理
  198.         if (day_week == 0) {
  199.             // 如果第一天是周日,则将后面的按钮上的文字都设为空
  200.             for (int i = days; i < 42; i++) {
  201.                 button_day[i].setText(" ");
  202.             }
  203.         } else {
  204.             // 如果第一天不是周日,则将第一天前面的按钮置空
  205.             for (int i = 0; i < day_week; i++) {
  206.                 button_day[i].setText(" ");
  207.             } // 最后一天后面的按钮置空
  208.             for (int i = day_week + days; i < 42; i++) {
  209.                 button_day[i].setText(" ");
  210.             }
  211.         }
  212.     }
  213.  
  214.     /**
  215.      * 处理事件
  216.      */
  217.     public void actionPerformed(ActionEvent e) {
  218.         if (e.getSource() == button_ok) {
  219.             // 如果点击确定按钮就调用setDay()重新方法绘制按钮
  220.             todayFlag = false;
  221.             this.paintDay();
  222.         } else if (e.getSource() == button_today) {
  223.             // 如果点击今天按钮,得到今天的日期
  224.             todayFlag = true;
  225.             YearBox.setSelectedIndex(10);
  226.             MonthBox.setSelectedIndex(now_month);
  227.             this.paintDay();
  228.         }
  229.     }
  230.  
  231.     public static void main(String[] args) {
  232.         CalenderTrain ct = new CalenderTrain();
  233.         ct.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  234.         ct.setVisible(true);
  235.         // ct.show();
  236.         // ct.addWindowListener(new WindowAdapter(){
  237.         // public void windowClosing(WindowEvent e){
  238.         // System.exit(1);
  239.         //
  240.         // }
  241.         // });
  242.     }
  243. }
  244.  

回复 "绘制日历"

这儿你可以回复上面这条便签

captcha