使用JavaGUI写一个日历

作为00后,直接上代码

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class CalendarGui implements ActionListener {
    private JFrame frame;
    private Container container;
    private JButton lastMonthBtn, nextMonthBtn;
    private JLabel yearLabel, monthLabel;
    private JPanel calendarPanel, topPanel, weekPanel, dayPanel;

    private Date currentDate;
    private SimpleDateFormat dateFormat;
    private Font font = new Font("宋体", Font.PLAIN, 16);
    private Color bgColor = Color.WHITE;
    private Color fgColor = Color.BLACK;
    private Color btnBgColor = Color.GRAY;
    private Color btnFgColor = Color.WHITE;

    public CalendarGui() {
        // 初始化日期格式
        currentDate = new Date();
        dateFormat = new SimpleDateFormat("yyyy-MM");
        
        // 创建主窗口
        frame = new JFrame("日历");
        frame.setSize(540, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        // 创建布局容器
        container = frame.getContentPane();
        container.setLayout(new BorderLayout());

        // 添加顶部面板
        topPanel = new JPanel(new FlowLayout());
        yearLabel = new JLabel();
        yearLabel.setFont(font);
        monthLabel = new JLabel();
        monthLabel.setFont(font);
        lastMonthBtn = new JButton("上个月");
        lastMonthBtn.setPreferredSize(new Dimension(100, 30));
        lastMonthBtn.addActionListener(this);
        nextMonthBtn = new JButton("下个月");
        nextMonthBtn.setPreferredSize(new Dimension(100, 30));
        nextMonthBtn.addActionListener(this);
        topPanel.add(lastMonthBtn);
        topPanel.add(yearLabel);
        topPanel.add(monthLabel);
        topPanel.add(nextMonthBtn);
        container.add(topPanel, BorderLayout.NORTH);

        // 添加日历面板        
        calendarPanel = new JPanel(new BorderLayout());
        weekPanel = new JPanel(new GridLayout(1, 7));
        String[] weeks = {"日", "一", "二", "三", "四", "五", "六"};
        for (int i = 0; i < 7; i++) {
            JLabel week = new JLabel(weeks[i], JLabel.CENTER);
            week.setFont(font);
            week.setForeground(fgColor);
            week.setBackground(bgColor);
            week.setOpaque(true);
            weekPanel.add(week);
        }
        calendarPanel.add(weekPanel, BorderLayout.NORTH);

        dayPanel = new JPanel(new GridLayout(6, 7));
        printCalendar(currentDate);
        calendarPanel.add(dayPanel, BorderLayout.CENTER);

        container.add(calendarPanel, BorderLayout.CENTER);

        // 显示窗口
        frame.setVisible(true);
    }

    /**
     * 打印指定月份的日历
     */
    private void printCalendar(Date date) {
        // 清空日历面板
        dayPanel.removeAll();

        // 更新日期标签
        yearLabel.setText(dateFormat.format(date).substring(0, 4) + "年");
        monthLabel.setText(dateFormat.format(date).substring(5) + "月");

        // 计算该月第一天是星期几
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, 1);
        int weekDay = calendar.get(Calendar.DAY_OF_WEEK) - 1;

        // 计算该月有多少天
        int daysOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

        // 打印日期
        for (int i = 0; i < weekDay; i++) {
            dayPanel.add(new JLabel());
        }
        for (int i = 1; i <= daysOfMonth; i++) {
            JButton dayBtn = new JButton(String.valueOf(i));
            dayBtn.setPreferredSize(new Dimension(70, 50));
            dayBtn.setBackground(btnBgColor);
            dayBtn.setForeground(btnFgColor);
            dayBtn.setFont(font);
            dayBtn.addActionListener(this);
            dayPanel.add(dayBtn);
        }

        // 更新日历面板
        dayPanel.updateUI();
    }

    /**
     * 处理按钮点击事件
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == lastMonthBtn) {
            // 上个月
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(currentDate);
            calendar.add(Calendar.MONTH, -1);
            currentDate = calendar.getTime();
            printCalendar(currentDate);
        } else if (e.getSource() == nextMonthBtn) {
            // 下个月
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(currentDate);
            calendar.add(Calendar.MONTH, 1);
            currentDate = calendar.getTime();
            printCalendar(currentDate);
        } else {
            // 点击日期
            JButton dayBtn = (JButton) e.getSource();
            int day = Integer.valueOf(dayBtn.getText());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(currentDate);
            calendar.set(Calendar.DATE, day);
            Date selectedDate = calendar.getTime();
            System.out.println(selectedDate);
        }
    }

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

这个日历程序使用Java Swing界面库编写,可以显示当前月份的日历,并支持通过上下按钮切换月份。用户也可以点击某一天,从而获取这一天的日期。主要实现逻辑如下:

  1. 创建一个JFrame窗口,设置布局容器为BorderLayout。
  2. 在顶部添加一个面板,包含上下按钮和年月标签。
  3. 在中间添加一个面板,用于显示日历。
  4. 打印指定月份的日历:先清空日历面板,然后计算该月第一天是星期几和该月的天数。接着循环打印日期,将每个日期都设置成一个带有文本的JButton,添加到一个GridLayout布局的面板中。最后更新日历面板。
  5. 处理按钮点击事件:点击上下按钮时更新当前日期并重新打印日历;点击日期时将选中的日期输出到控制台。
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

个人练习生xx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值