温馨提示×

怎么用Java写一个带有窗体的日历

小亿
99
2024-01-13 02:16:50
栏目: 编程语言

要用Java写一个带有窗体的日历,可以使用Java的Swing库来创建窗体和组件。

下面是一个简单的示例代码,展示了如何使用Java编写一个带有窗体的日历:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;
import java.time.YearMonth;

public class CalendarApp extends JFrame {
    private YearMonth currentYearMonth;
    private JLabel monthLabel;
    private JButton prevButton;
    private JButton nextButton;
    private JTable calendarTable;

    public CalendarApp() {
        currentYearMonth = YearMonth.now();

        setTitle("Calendar App");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        monthLabel = new JLabel("", JLabel.CENTER);
        calendarTable = new JTable(6, 7);

        prevButton = new JButton("Prev");
        prevButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                currentYearMonth = currentYearMonth.minusMonths(1);
                updateCalendar();
            }
        });

        nextButton = new JButton("Next");
        nextButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                currentYearMonth = currentYearMonth.plusMonths(1);
                updateCalendar();
            }
        });

        updateCalendar();

        setLayout(new BorderLayout());
        add(monthLabel, BorderLayout.NORTH);
        add(new JScrollPane(calendarTable), BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(prevButton);
        buttonPanel.add(nextButton);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    private void updateCalendar() {
        monthLabel.setText(currentYearMonth.getYear() + "年" + currentYearMonth.getMonthValue() + "月");

        LocalDate firstDayOfMonth = currentYearMonth.atDay(1);
        int firstDayOfWeek = firstDayOfMonth.getDayOfWeek().getValue();

        int daysInMonth = currentYearMonth.lengthOfMonth();
        int numRows = (int) Math.ceil((firstDayOfWeek + daysInMonth) / 7.0);

        String[] columnNames = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Object[][] data = new Object[numRows][7];

        int day = 1 - firstDayOfWeek;
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < 7; j++) {
                if (day > 0 && day <= daysInMonth) {
                    data[i][j] = day;
                } else {
                    data[i][j] = "";
                }
                day++;
            }
        }

        calendarTable.setModel(new javax.swing.table.DefaultTableModel(data, columnNames));
        calendarTable.setDefaultRenderer(calendarTable.getColumnClass(0), new CalendarCellRenderer());
    }

    private class CalendarCellRenderer extends DefaultTableCellRenderer {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            if (value != null) {
                int day = (int) value;
                if (day == LocalDate.now().getDayOfMonth() && currentYearMonth.equals(YearMonth.now())) {
                    label.setForeground(Color.RED);
                } else {
                    label.setForeground(table.getForeground());
                }
            }
            return label;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new CalendarApp().setVisible(true);
            }
        });
    }
}

这个示例代码创建了一个带有窗体的日历应用,可以通过Prev和Next按钮切换月份。日历表格中,当前日期会以红色显示。

在这个示例代码中,我们使用了Swing的JFrame、JLabel、JButton、JTable等组件来构建窗体和日历表格。updateCalendar()方法用于更新日历表格的内容。CalendarCellRenderer类用于自定义单元格的渲染,以便高亮显示当前日期。

通过运行main()方法,可以启动日历应用并显示窗体。

0