java写日历(源码)

用到java图形交互的基础知识,可以练练手。 

import java.awt.*;
import java.awt.event.*;
import java.text.DateFormatSymbols;
import java.util.*;
import javax.swing.*;

public class WindowCalendar extends JFrame implements ActionListener {
    private JButton previousButton, nextButton;
    private JComboBox monthComboBox, yearComboBox;
    private JLabel monthLabel, yearLabel;
    private JPanel calendarPanel;

    public WindowCalendar() {
        setTitle("日历");

        // 创建月份下拉框
        String[] monthStrings = new DateFormatSymbols().getMonths();
        monthComboBox = new JComboBox(monthStrings);
        monthComboBox.setSelectedIndex(Calendar.getInstance().get(Calendar.MONTH));
        monthComboBox.addActionListener(this);

        // 创建年份下拉框
        Integer[] yearIntegers = new Integer[200];
        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
        //System.out.println(currentYear);
        for (int i = 0; i < yearIntegers.length; i++) {
            yearIntegers[i] = new Integer(currentYear - 122 + i);
          //  System.out.println(yearIntegers[i]);
        }
        yearComboBox = new JComboBox(yearIntegers);
        yearComboBox.setSelectedItem(new Integer(currentYear));
        yearComboBox.addActionListener(this);

        // 创建月份和年份标签
        monthLabel = new JLabel("Month:");
        yearLabel = new JLabel("Year:");

        // 创建上一个和下一个按钮
       previousButton = new JButton("<");
       previousButton.addActionListener(this);
       nextButton = new JButton(">");
       nextButton.addActionListener(this);

        // 创建日历面板
        calendarPanel = new JPanel(new GridLayout(6, 7));
        calendarPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        updateCalendar();

        // 将组件添加到容器中
        JPanel topPanel = new JPanel();
        topPanel.add(monthLabel);
        topPanel.add(monthComboBox);
        topPanel.add(yearLabel);
        topPanel.add(yearComboBox);
        topPanel.add(previousButton);
        topPanel.add(nextButton);
        add(topPanel, BorderLayout.NORTH);
        add(calendarPanel, BorderLayout.CENTER);
        // 设置窗口属性
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent event) {
        Object source = event.getSource();
        if (source == previousButton) {
            moveBackwards();
            updateCalendar();
        }
        else if (source == nextButton) {
            moveForwards();
            updateCalendar();
        }
        else if (source == monthComboBox || source == yearComboBox) {
            updateCalendar();
        }
    }
    private void moveBackwards() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(yearComboBox.getSelectedIndex() + 1900, monthComboBox.getSelectedIndex(), 1);
        calendar.add(Calendar.MONTH, -1);
        monthComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));
        yearComboBox.setSelectedItem(new Integer(calendar.get(Calendar.YEAR)));
    }
    private void moveForwards() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(yearComboBox.getSelectedIndex() + 1900, monthComboBox.getSelectedIndex(), 1);
        calendar.add(Calendar.MONTH, 1);
        monthComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));
        yearComboBox.setSelectedItem(new Integer(calendar.get(Calendar.YEAR)));
    }

    private void updateCalendar() {
        int year = ((Integer)yearComboBox.getSelectedItem()).intValue();
        int month = monthComboBox.getSelectedIndex();
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, 1);

        // 清除日历面板上的所有组件
        calendarPanel.removeAll();

        // 添加星期标签
        String[] weekdayStrings = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        for (int i = 0; i < weekdayStrings.length; i++) {
            JLabel label = new JLabel(weekdayStrings[i], JLabel.CENTER);
            calendarPanel.add(label);
        }

        // 添加日期按钮
        int weekday = calendar.get(Calendar.DAY_OF_WEEK);
        int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int i = 1; i < weekday; i++) {
            JLabel label = new JLabel();
            calendarPanel.add(label);
        }
        for (int i = 1; i <= maxDays; i++) {
            JButton button = new JButton(String.valueOf(i));
            button.addActionListener(new ButtonListener());
            calendarPanel.add(button);
        }

        // 重新绘制日历
        revalidate();
        repaint();
    }

    private class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            JOptionPane.showMessageDialog(WindowCalendar.this, "You clicked on day " + ((JButton)event.getSource()).getText());
        }
    }

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

效果图:

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值