⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datecalendarpanel.java

📁 本次实验中用Java实现的Calendar具有跨平台运行能力
💻 JAVA
字号:
package edu.swjtu.sist.java.calendar;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class DateCalendarPanel extends JPanel
{

    public static final String WEEK_SUN = "日";

    public static final String WEEK_MON = "一";

    public static final String WEEK_TUE = "二";

    public static final String WEEK_WED = "三";

    public static final String WEEK_THU = "四";

    public static final String WEEK_FRI = "五";

    public static final String WEEK_SAT = "六";

    public static final Color background = Color.white;

    public static final Color foreground = Color.black;

    public static final Color headerBackground = SystemColor.inactiveCaption;

    public static final Color headerForeground = Color.white;

    public static final Color selectedBackground = SystemColor.activeCaption;

    public static final Color selectedForeground = Color.white;

    private JSpinner yearsSpinner;

    private JComboBox monthsComboBox;

    private JTable daysTable;

    private AbstractTableModel daysModel;

    protected Calendar calendar;

    public DateCalendarPanel()
    {
        this.setLayout(new BorderLayout());

        calendar = Calendar.getInstance();

        yearsSpinner = new JSpinner();
        yearsSpinner.setEditor(new JSpinner.NumberEditor(yearsSpinner, "0000"));
        yearsSpinner.setValue(new Integer(calendar.get(Calendar.YEAR)));
        yearsSpinner.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent changeEvent)
            {
                int day = calendar.get(Calendar.DAY_OF_MONTH);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                calendar.set(Calendar.YEAR, ((Integer) yearsSpinner.getValue())
                        .intValue());
                int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                calendar
                        .set(Calendar.DAY_OF_MONTH, day > maxDay ? maxDay : day);
                updateView();
            }
        });

        JPanel yearMonthPanel = new JPanel();
        this.add(yearMonthPanel, BorderLayout.NORTH);
        yearMonthPanel.setLayout(new BorderLayout());
        JPanel yearPanel = new JPanel();
        // 先添加月在添加年,和windows日历一致
        yearPanel.setLayout(new BorderLayout());
        yearPanel.add(yearsSpinner, BorderLayout.CENTER);

        monthsComboBox = new JComboBox();

        monthsComboBox.addItem("一月");
        monthsComboBox.addItem("二月");
        monthsComboBox.addItem("三月");
        monthsComboBox.addItem("四月");
        monthsComboBox.addItem("五月");
        monthsComboBox.addItem("六月");
        monthsComboBox.addItem("七月");
        monthsComboBox.addItem("八月");
        monthsComboBox.addItem("九月");
        monthsComboBox.addItem("十月");
        monthsComboBox.addItem("十一月");
        monthsComboBox.addItem("十二月");

        monthsComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));
        monthsComboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent)
            {
                int day = calendar.get(Calendar.DAY_OF_MONTH);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                calendar.set(Calendar.MONTH, monthsComboBox.getSelectedIndex());
                int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                calendar
                        .set(Calendar.DAY_OF_MONTH, day > maxDay ? maxDay : day);
                updateView();
            }
        });
        JPanel monthPanel = new JPanel();
        // 先添加月在添加年,和windows日历一致
        yearMonthPanel.add(monthPanel, BorderLayout.WEST);
        yearMonthPanel.add(yearPanel, BorderLayout.EAST);
        yearMonthPanel.setBackground(SystemColor.controlHighlight);
        monthPanel.setLayout(new BorderLayout());
        monthPanel.add(monthsComboBox, BorderLayout.CENTER);

        daysModel = new AbstractTableModel() {
            public int getRowCount()
            {
                return 7;
            }

            public int getColumnCount()
            {
                return 7;
            }

            public Object getValueAt(int row, int column)
            {
                if (row == 0)
                {
                    return getHeader(column);
                }
                row--;
                Calendar calendar = (Calendar) DateCalendarPanel.this.calendar
                        .clone();
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                int dayCount = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                int moreDayCount = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                int index = row * 7 + column;
                int dayIndex = index - moreDayCount + 1;
                if (index < moreDayCount || dayIndex > dayCount)
                {
                    return null;
                }
                else
                {
                    return new Integer(dayIndex);
                }
            }
        };

        daysTable = new CalendarTable(daysModel, calendar);
        daysTable.setCellSelectionEnabled(true);
        daysTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        daysTable.setDefaultRenderer(daysTable.getColumnClass(0),
                new TableCellRenderer() {
                    public Component getTableCellRendererComponent(
                            JTable table, Object value, boolean isSelected,
                            boolean hasFocus, int row, int column)
                    {
                        String text = (value == null) ? "" : value.toString();
                        JLabel cell = new JLabel(text);
                        cell.setOpaque(true);
                        if (row == 0)
                        {
                            cell.setForeground(headerForeground);
                            cell.setBackground(headerBackground);
                        }
                        else
                        {
                            if (isSelected)
                            {
                                cell.setForeground(selectedForeground);
                                cell.setBackground(selectedBackground);
                            }
                            else
                            {
                                cell.setForeground(foreground);
                                cell.setBackground(background);
                            }
                        }

                        return cell;
                    }
                });
        updateView();

        this.add(daysTable, BorderLayout.CENTER);
    }

    public static String getHeader(int index)
    {
        switch (index)
        {
        case 0:
            return WEEK_SUN;
        case 1:
            return WEEK_MON;
        case 2:
            return WEEK_TUE;
        case 3:
            return WEEK_WED;
        case 4:
            return WEEK_THU;
        case 5:
            return WEEK_FRI;
        case 6:
            return WEEK_SAT;
        default:
            return null;
        }
    }

    public void updateView()
    {
        daysModel.fireTableDataChanged();
        daysTable.setRowSelectionInterval(calendar.get(Calendar.WEEK_OF_MONTH),
                calendar.get(Calendar.WEEK_OF_MONTH));
        daysTable.setColumnSelectionInterval(
                calendar.get(Calendar.DAY_OF_WEEK) - 1, calendar
                        .get(Calendar.DAY_OF_WEEK) - 1);
    }

    public static class CalendarTable extends JTable
    {

        private Calendar calendar;

        public CalendarTable(TableModel model, Calendar calendar)
        {
            super(model);
            this.calendar = calendar;
            this.setRowMargin(0);

            this.getColumnModel().setColumnMargin(0);

        }

        public void changeSelection(int row, int column, boolean toggle,
                boolean extend)
        {
            super.changeSelection(row, column, toggle, extend);
            if (row == 0)
            {
                return;
            }
            Object obj = getValueAt(row, column);
            if (obj != null)
            {
                calendar.set(Calendar.DAY_OF_MONTH, ((Integer) obj).intValue());
                //System.out.println(calendar.getTime());
                WindowsTime.hasModified = true;
            }
        }

    }

    public static void main(String[] args)
    {
        JFrame frame = new JFrame("日历应用程序");
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (ClassNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (InstantiationException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IllegalAccessException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (UnsupportedLookAndFeelException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        DateCalendarPanel myCalendar = new DateCalendarPanel();
        //myCalendar.init();
        frame.getContentPane().add(myCalendar);
        frame.setSize(162, 160);

        frame.setVisible(true);
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -