自制日历java

package pac02;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.*;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.util.Iterator;
import java.util.LinkedList;

public class MyCourseDesign {
    public static void main(String[] args) {
        new InitWindow();
    }
}

class MyTable implements ActionListener{        //view
    JButton button;         //按钮确定输入
    JButton nextMonth;
    JButton previousMonth;
    JButton TableBack;
    JTextField inputYear;   //输入年份
    JComboBox<String>inputmonth; //下拉框选择月份
    MyPolice police;        //确定按钮的监视器
    JLabel  yearLabel;      //提示年的标签
    JLabel monthLabel;      //提示月的标签
    JLabel NowLabel;
    JButton nowButton;
    MyCalnadar myCalnadar;    //显示日历的主体
    JPanel northPanel;
    JPanel centralPanel;
    JPanel southPanel;
    JLayeredPane layeredPane;
    JPanel allPanel;
    LocalDateTime timeNow;
    Timer time;
    Box panelBox;
    InitWindow win;

    MyTable(){
        init();
        allPanel.setVisible(true);
        allPanel.setBounds(200,200,700,500);
    }

    public void setMyFont(Font font,JComponent...x){
        for(JComponent c:x){
            c.setFont(font);
        }
    }

    public void init(){
        BorderLayout bord = new BorderLayout();
        allPanel = new JPanel();
        allPanel.setLayout(bord);
        TableBack = new JButton("返回");
        panelBox = Box.createVerticalBox();
        nextMonth = new JButton(">>");
        previousMonth = new JButton("<<");
        myCalnadar = new MyCalnadar();
        police = new MyPolice();
        time = new Timer(1000,this);
        time.start();
        button  =new JButton("显示");
        inputmonth = new JComboBox<>();
        inputYear = new JTextField(6);
        yearLabel  = new JLabel("输入年:");
        monthLabel  = new JLabel("请选择月:");
        northPanel = new JPanel();
        centralPanel = new JPanel();
        southPanel = new JPanel();
        allPanel = new JPanel();
        layeredPane = new JLayeredPane();
        String []s1=new String[12];
        timeNow = LocalDateTime.now();
        NowLabel = new JLabel("现在的时间:"+timeNow.getYear()+"年"+timeNow.getMonth()+"月"+timeNow.getDayOfMonth()+"日"+timeNow.getHour()+":"+timeNow.getMinute()+":"+timeNow.getSecond());
        for (int i = 1; i <= 12; i++) {
            s1[i-1] = new String(String.valueOf(i));
            inputmonth.addItem(s1[i-1]);
        }
        setWin(win);
        police.setView(this);
        myCalnadar.setView(this);
        myCalnadar.setPanel(centralPanel);
        button.addActionListener(police);
        Font font = new Font("宋体",Font.BOLD,20);
        Font font01 = new Font("楷体",Font.BOLD,30);
        setMyFont(font,yearLabel,monthLabel,inputYear,inputmonth,button,nextMonth,previousMonth,NowLabel,button,TableBack);
        northPanel.add(TableBack);
        northPanel.add(yearLabel);
        northPanel.add(inputYear);
        northPanel.add(monthLabel);
        northPanel.add(inputmonth);
        northPanel.add(button);
        northPanel.add(previousMonth);
        northPanel.add(nextMonth);

        TableBack.addActionListener(police);

        previousMonth.addActionListener(police);
        nextMonth.addActionListener(police);
        nowButton= new JButton("");
        nowButton.setFont(font01);
        panelBox.add(nowButton);
        panelBox.add(Box.createVerticalStrut(4));
        panelBox.add(NowLabel);
        southPanel.add(panelBox);
        allPanel.add(northPanel,BorderLayout.NORTH);
        allPanel.add(centralPanel,BorderLayout.CENTER);
        allPanel.add(southPanel,BorderLayout.SOUTH);

        //初始时使日历显示2021年1月的日历
        inputYear.setText(2021+"");
        myCalnadar.setYear(2021);
        myCalnadar.setMonth(1);
        myCalnadar.showCalnadar();
        nowButton.setText("2021年1月日历");
    }

    public void actionPerformed(ActionEvent ae){
        //实现计时器功能
        time.start();
        LocalDateTime timeNow = LocalDateTime.now();
        NowLabel.setText("现在的时间:"+timeNow.getYear()+"年"+police.Convers(timeNow.getMonth())+"月"+timeNow.getDayOfMonth()+"日"+timeNow.getHour()+":"+timeNow.getMinute()+":"+timeNow.getSecond());
    }

    public void setWin(InitWindow win){
        this.win = win;
    }

    public void setPanel(JPanel panel){
        this.allPanel=panel;
    }

}

class MyCalnadar{               //Module
    int year;
    int month;
    GridLayout grid;
    JPanel panel;
    MyTable view;
    public void setView(MyTable view){
        this.view=view;
    }

    public void setPanel(JPanel panel){
        this.panel = panel;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getYear() {
        return year;
    }

    public int getMonth() {
        return month;
    }

    public int PrintSpace(DayOfWeek x){
        switch (x){
            case MONDAY -> {
                return 0;
            }
            case TUESDAY -> {
                return 1;
            }
            case WEDNESDAY -> {
                return 2;
            }
            case THURSDAY -> {
                return 3;
            }
            case FRIDAY -> {
                return 4;
            }
            case SATURDAY -> {
                return 5;
            }
            case SUNDAY -> {
                return 6;
            }
            default -> {
                return -1;
            }
        }
    }

    public void showCalnadar(){
        Font font02 = new Font("楷体",Font.PLAIN,30);
        panel.setFont(font02);
        panel.setSize(100,100);
        Font font01 = new Font("宋体",Font.BOLD,20);
        panel.removeAll();
        grid = new GridLayout(7,7);
        panel.setLayout(grid);
        LocalDate date = LocalDate.of(year,month,1);
        int days = date.lengthOfMonth();
        LocalDate[]dateArray = new LocalDate[days];
        for (int i = 0; i < days; i++) {
            dateArray[i] =LocalDate.of(year,month,date.getDayOfMonth()+i);
        }
        String []name = new String[]{"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
        //发现显示不了中文
        Label []titleWeek= new Label[7];
        Label []showDay = new Label[42];
        for(int i=0;i<7;i++){
            titleWeek[i] = new Label("",Label.CENTER);
            titleWeek[i].setText(name[i]+"");
            titleWeek[i].setFont(font01);
            panel.add(titleWeek[i]);
        }
        for(int i=0;i<42;i++){
            showDay[i] = new Label("",Label.CENTER);
            panel.add(showDay[i]);
        }
        int spaceOfMonth = PrintSpace(dateArray[0].getDayOfWeek());
        for (int i = 0; i < spaceOfMonth; i++) {
            showDay[i].setText("");
        }
        for (int i = spaceOfMonth,j=0; j < days; j++,i++) {
            showDay[i].setText(""+dateArray[j].getDayOfMonth());
        }
        panel.repaint();
    }
}

class MyPolice implements ActionListener {     //Controller
    MyTable view;
    int year,month;
    boolean isShow=true;
    boolean canEdit = true;
    Font font = new Font("宋体",Font.BOLD,17);
    public void setView(MyTable view){
        this.view=view;
    }
    public void actionPerformed(ActionEvent ae){
        if(ae.getSource()== view.TableBack){
            view.win.card.show(view.win.allPanel,"firstPanel");
        }
        if(ae.getSource()==view.button){
            //显示日历的主体
            if(view.inputYear.getText()==null){
                //如果根本没有输入年份
                canEdit = false;
                JLabel label1 = new JLabel("请输入年份!!!");
                label1.setFont(font);
                label1.setBackground(Color.RED);
                JOptionPane.showMessageDialog(view.allPanel,label1,"提示输入提醒",JOptionPane.ERROR_MESSAGE);
            }
            else{
                canEdit = true;
            }
            try{
                //尝试看看输入的年份有没有其他字符
                year = Integer.parseInt(view.inputYear.getText());
                canEdit = true;
            }
            catch (NumberFormatException exp){
                canEdit = false;
                JLabel label1 = new JLabel("输入的年份不要包含除数字之外的其他格式!!!");
                label1.setFont(font);
                view.inputYear.setText(2021+"");
                label1.setForeground(Color.RED);
                JOptionPane.showMessageDialog(view.allPanel,label1,"输入格式提醒",JOptionPane.ERROR_MESSAGE);
                isShow=false;
                view.inputYear.requestFocus();
            }
            if(view.inputYear.getText()!=null&&(year>3500||year<1500)){
                //检查输入的年份有没有超出范围
                canEdit = false;
                JLabel label1 = new JLabel("年份超出范围!请在1500-3500之间输入");
                label1.setFont(font);
                view.inputYear.setText("2021");
                label1.setForeground(Color.RED);
                JOptionPane.showMessageDialog(view.allPanel,label1,"超出范围提醒",JOptionPane.ERROR_MESSAGE);
                isShow=false;
//                view.inputYear.setText(null);
                view.inputYear.requestFocus();
            }
            else{
                canEdit = true;
            }
            try{
                //操作选中的月份
                int month = Integer.parseInt(view.inputmonth.getSelectedItem().toString());
                canEdit = true;
            }
            catch (Exception exp){
                canEdit = false;
                JLabel label1 = new JLabel("您还未选择月份!!!");
                label1.setFont(font);
                label1.setForeground(Color.RED);
                JOptionPane.showMessageDialog(view.allPanel,label1,"提示选择提醒",JOptionPane.ERROR_MESSAGE);
                isShow=false;
            }
            //根据输入的数据显示日历
            int year = Integer.parseInt(view.inputYear.getText());
            int month = Integer.parseInt(view.inputmonth.getSelectedItem().toString());
            view.myCalnadar.setYear(year);
            view.myCalnadar.setMonth(month);
        }
        if(ae.getSource()==view.nextMonth){
            //按按钮以显示下一个月的日历
            if(canEdit){
                int year = view.myCalnadar.getYear();
                int month = view.myCalnadar.getMonth();
                if(month==12){
                    view.myCalnadar.setYear(year+1);
                    view.myCalnadar.setMonth(1);
                }
                else{
                    view.myCalnadar.setMonth(month+1);
                }
            }
            else{
                JLabel label1 = new JLabel("请先显示日历后再按按钮挑战月份!");
                label1.setFont(font);
                label1.setForeground(Color.RED);
                JOptionPane.showMessageDialog(view.allPanel,label1,"错误选择提醒",JOptionPane.ERROR_MESSAGE);
            }
        }
        if(ae.getSource()== view.previousMonth){
            //按按钮以显示上一个月的日历
            if(canEdit){
                int year = view.myCalnadar.getYear();
                int month = view.myCalnadar.getMonth();
                if(month==1){
                    view.myCalnadar.setYear(year-1);
                    view.myCalnadar.setMonth(12);
                }
                else{
                    view.myCalnadar.setMonth(month-1);
                }
            }
            else {
                JLabel label1 = new JLabel("请先显示日历后再按按钮挑战月份!");
                label1.setFont(font);
                label1.setForeground(Color.RED);
                JOptionPane.showMessageDialog(view.allPanel,label1,"错误选择提醒",JOptionPane.ERROR_MESSAGE);
            }
        }
        if(isShow&canEdit){
            //根据有没有错误数据来显示日历主体
            view.myCalnadar.showCalnadar();
            view.nowButton.setText(view.myCalnadar.getYear()+"年"+ view.myCalnadar.getMonth()+"月的日历");
            view.nowButton.setBackground(Color.WHITE);
        }
        isShow=true;
    }
    public int Convers(Month m){
        switch (m){
            case JANUARY -> {
                return 1;
            }
            case FEBRUARY -> {
                return 2;
            }
            case MARCH -> {
                return 3;
            }
            case APRIL -> {
                return 4;
            }
            case MAY -> {
                return 5;
            }
            case JUNE -> {
                return 6;
            }
            case JULY -> {
                return 7;
            }
            case AUGUST -> {
                return 8;
            }
            case SEPTEMBER -> {
                return 9;
            }
            case OCTOBER -> {
                return 10;
            }
            case NOVEMBER -> {
                return 11;
            }
            case DECEMBER -> {
                return 12;
            }
            default -> {
                return -1;
            }
        }
    }
}

class InitWindow extends JFrame implements ActionListener{
    JButton showCal;
    JButton showMes;
    JButton showOpe;
    JPanel CalPanel;
    JPanel MesPanel;
    JPanel OpePanel;
    JLabel imageLabel;
    JPanel imagePanel;
    JPanel allPanel;
    JPanel firstPanel;
    JLayeredPane layeredPane;
    ImageIcon icon;
    Box baseBox;
    Box box1;
    Box box2;
    Font font,font01;
    JLabel hiLabel;
    JLabel opeLabel;
    JLabel spaceLabel;
    JLabel imaLabel;
    CardLayout card;
    MessageWindow mesWin;
    MyTable tabWin;
    OperateWindow opeWin;

    InitWindow(){
        init();
        setBounds(200,200,700,500);
        setVisible(true);
        setTitle("自制多功能日历器");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void init(){
        mesWin = new MessageWindow();
        mesWin.setView(this);
        tabWin = new MyTable();
        tabWin.setWin(this);
        opeWin = new OperateWindow();
        opeWin.setView((this));
        card = new CardLayout();
        CalPanel = new JPanel();
        OpePanel = new JPanel();
        MesPanel = new JPanel();
        allPanel = new JPanel();
        firstPanel = new JPanel();
        allPanel.setLayout(card);
        allPanel.add("OpePanel",opeWin.allPanel);
        allPanel.add("MesPanel",mesWin.allPanel);
        allPanel.add("CalPanel",tabWin.allPanel);
        allPanel.add("firstPanel",firstPanel);
        //对应3个窗口
        showCal = new JButton("查看日历");
        showMes = new JButton("查看节日");
        showOpe = new JButton("查看日程");
        showOpe.addActionListener(this);
        showMes.addActionListener(this);
        showCal.addActionListener(this);

        font = new Font("微软雅黑",Font.ITALIC,20);
        font01 = new Font("宋体",Font.PLAIN,50);
        hiLabel = new JLabel("你好,Calendar");
//        imagePanel.setSize(icon.getIconWidth(),icon.getIconHeight());
//        imagePanel.add(imageLabel,BorderLayout.CENTER);

        setBox();

//        layeredPane.add(imagePanel,JLayeredPane.MODAL_LAYER);
//        layeredPane.add(baseBox,JLayeredPane.DEFAULT_LAYER);
        firstPanel.add(baseBox);
//        opePanel.add(hiLabel,BorderLayout.NORTH);
//        setLayeredPane(layeredPane);
        card.show(allPanel,"firstPanel");
        setMyFont(font,showOpe,showMes,showCal,spaceLabel,opeLabel);
        hiLabel.setFont(font01);
        add(allPanel);
        mesWin = new MessageWindow();

//        this.MesPanel = mesWin.allPanel;
    }

    public void setMyFont(Font font,JComponent...x){
        for(JComponent c:x){
            c.setFont(font);
        }
    }

    public void setBox(){
        opeLabel = new JLabel("功能选项");
        spaceLabel = new JLabel("               ");
        box1 = Box.createVerticalBox();
        box1.add(Box.createVerticalStrut(50));
        box1.add(opeLabel);
        box1.add(Box.createVerticalStrut(25));
        box1.add(showCal);
        box1.add(Box.createVerticalStrut(50));
        box1.add(showMes);
        box1.add(Box.createVerticalStrut(50));
        box1.add(showOpe);
        box2 = Box.createVerticalBox();
//        box2.add(Box.createVerticalStrut(50));
        box2.add(hiLabel);
        box2.add(Box.createVerticalStrut(50));
        box2.add(spaceLabel);
        box2.add(Box.createVerticalStrut(50));
        box2.add(spaceLabel);
        baseBox = Box.createHorizontalBox();
        baseBox.add(box1);
        baseBox.add(Box.createHorizontalStrut(100));
        baseBox.add(box2);
        baseBox.setOpaque(false);
        box1.setOpaque(false);
        box2.setOpaque(false);
    }

    public void actionPerformed(ActionEvent ae){
        if(ae.getSource()==showCal){
            card.show(allPanel,"CalPanel");
        }
        if(ae.getSource()==showMes){
            card.show(allPanel,"MesPanel");
        }
        if(ae.getSource()==showOpe){
            card.show(allPanel,"OpePanel");
        }
    }
}

class MessageWindow implements ActionListener{    //还有节假日的图片与信息没填
    Box baseBox;
    Box box1;
    Box box2;
    InitWindow view;
    JLabel imageLabelOfSolar;
    JLabel imageLabelOfFesti;
    JLabel showLabel;
    CardLayout card = null;
    JTextArea showAreaSolar;
    JTextArea showAreaFesti;
    JPanel panelOfSolar;
    JPanel panelOfFesti;
    JPanel allPanel;   //在主窗口显示的面板,以cardlayout布局
    JPanel firstPanel;
    ImageIcon icon;
    JComboBox<String>comboBoxOfSolar;
    JComboBox<String>comboBoxOfFesti;
    JButton enterSolar;
    JButton enterFesti;
    JButton solarButton,festiButton;
    JButton solarToFirst,festiToFirst,firstToMain;
    Font font;
    File file=null;
    FileReader read=null;
    BufferedReader reader=null;
    MessageWindow(){
        init();
        allPanel.setBounds(200,200,700,500);
//        allPanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        allPanel.setVisible(true);
//        allPanel.setTitle("查看节气");
        allPanel.validate();
    }
    public void init(){
        font = new Font("微软雅黑",Font.PLAIN,20);
        card = new CardLayout();
        setSolarPanel();
        setFestiPanel();
        setFirstPanel();
        allPanel = new JPanel();
        allPanel.setLayout(card);
        allPanel.add("solar",panelOfSolar);
        allPanel.add("festi",panelOfFesti);
        allPanel.add("first",firstPanel);
        card.show(allPanel,"first");
        enterFesti.addActionListener(this);
        enterSolar.addActionListener(this);
        solarButton.addActionListener(this);
        festiButton.addActionListener(this);
        solarToFirst.addActionListener(this);
        festiToFirst.addActionListener(this);
        setMyFont(font,solarButton,festiButton,solarToFirst,festiToFirst,firstToMain);
    }
    public void actionPerformed(ActionEvent ae){
        if(ae.getSource()==firstToMain){
            view.card.show(view.allPanel,"firstPanel");
        }
        if(ae.getSource()==solarButton){
            card.show(allPanel,"solar");
        }
        if(ae.getSource()==festiButton){
            card.show(allPanel,"festi");
        }
        if(ae.getSource()==solarToFirst){
            card.show(allPanel,"first");
        }
        if(ae.getSource()==festiToFirst){
            card.show(allPanel,"first");
        }
        if(ae.getSource()==enterFesti){
            //这里要用文件操作
            showAreaFesti.setText(null);
            String str = comboBoxOfFesti.getSelectedItem().toString();
            icon = new ImageIcon(str+".png");
            imageLabelOfFesti.setIcon(icon);
            file = new File("festival.txt");
            try{
                read = new FileReader(file);
                reader = new BufferedReader(read);
                String tar=null;
                while((tar=reader.readLine())!=null){
                    if(tar.equals("0000"+str)){
                        String content = reader.readLine();
                        for (int i = 0; i < content.length(); i++) {
                            showAreaFesti.append(""+content.charAt(i));
                            if(i%8==0){
                                showAreaFesti.append("\n");
                            }
                        }
                    }
                }
                reader.close();
                read.close();
            }
            catch (IOException ioexp){
                showAreaSolar.setText("无资料");
            }
        }
        if(ae.getSource()==enterSolar){
            showAreaSolar.setText(null);
            String str = comboBoxOfSolar.getSelectedItem().toString();
            icon = new ImageIcon(str+".png");
            imageLabelOfSolar.setIcon(icon);
            file = new File("solar.txt");
            try{
                read = new FileReader(file);
                reader = new BufferedReader(read);
                String tar=null;
                while((tar=reader.readLine())!=null){
                    if(tar.equals("0000"+str)){
                        String content = reader.readLine();
                        for (int i = 0; i < content.length(); i++) {
                            showAreaSolar.append(""+content.charAt(i));
                            if(i%8==0){
                                showAreaSolar.append("\n");
                            }
                        }
                    }
                }
                reader.close();
                read.close();
            }
            catch (IOException ioexp){
                showAreaSolar.setText("无资料");
            }
            //showArea里面的内容也要用到文件操作
        }
    }

    public void setMyFont(Font font,JComponent...x){
        for(JComponent c:x){
            c.setFont(font);
        }
    }

    public void setSolarPanel(){
        solarToFirst = new JButton("返回");
        panelOfSolar = new JPanel();
        BorderLayout bord = new BorderLayout();
//        panelOfSolar.setLayout(bord);
        enterSolar = new JButton("确定");
        //二十四节气面板加入box和显示图片的label
        baseBox = Box.createHorizontalBox();
        showAreaSolar = new JTextArea(10,10);
        showAreaSolar.setEditable(false);
        imageLabelOfSolar = new JLabel();
        imageLabelOfSolar.setSize(250,300);
        /
//        icon = new ImageIcon("原始.png");
//        imageLabelOfSolar.setIcon(icon);
        /
        showLabel = new JLabel("请选择节气");
        comboBoxOfSolar = new JComboBox<>();
        String[]solar={"立春","雨水","惊蛰","春分","清明","谷雨","立夏","小满","芒种","夏至","小暑","大暑","立秋","处暑","白露","秋分","寒露","霜降","立冬","小雪","大雪","冬至","小寒","大寒"};
        for (String s : solar) {
            comboBoxOfSolar.addItem(s);
        }
        box1 = Box.createHorizontalBox();
        box1.add(new JScrollPane(showAreaSolar));
        box1.add(Box.createHorizontalStrut(12));
        box1.add(imageLabelOfSolar);
        box2 = Box.createHorizontalBox();
        box2.add(solarToFirst);
        box2.add(Box.createHorizontalStrut(10));
        box2.add(showLabel);
        box2.add(Box.createHorizontalStrut(10));
        box2.add(comboBoxOfSolar);
        box2.add(Box.createHorizontalStrut(30));
        box2.add(enterSolar);
        baseBox = Box.createVerticalBox();
        baseBox.add(box2);
        baseBox.add(Box.createVerticalStrut(20));
        baseBox.add(box1);
        panelOfSolar.add(baseBox,BorderLayout.CENTER);
        setMyFont(font,showAreaSolar,showLabel,comboBoxOfSolar,enterSolar);
    }

    public void setFestiPanel(){
        festiToFirst = new JButton("返回");
        String[] festi={"元旦","春节","清明节","妇女节","植树节","劳动节","青年节","端午节","儿童节","建党节","建军节","教师节","中秋节","国庆节"};
        comboBoxOfFesti = new JComboBox<>();
        for(String s:festi){
            comboBoxOfFesti.addItem(s);
        }
        showAreaFesti = new JTextArea(10,10);
        showAreaFesti.setEditable(false);
        showLabel = new JLabel("请选择节日:");
        enterFesti = new JButton("确定");
        panelOfFesti = new JPanel();
        imageLabelOfFesti = new JLabel();
//        icon = new ImageIcon("原始.png");
//        imageLabelOfFesti.setIcon(icon);
        imageLabelOfFesti.setSize(250,300);
        box1 = Box.createHorizontalBox();
        box1.add(festiToFirst);
        box1.add(Box.createHorizontalStrut(10));
        box1.add(showLabel);
        box1.add(Box.createHorizontalStrut(10));
        box1.add(comboBoxOfFesti);
        box1.add(Box.createHorizontalStrut(30));
        box1.add(enterFesti);
        box2 = Box.createHorizontalBox();
        box2.add(new JScrollPane(showAreaFesti));
        box2.add(Box.createHorizontalStrut(20));
        box2.add(imageLabelOfFesti);
        baseBox = Box.createVerticalBox();
        baseBox.add(box1);
        baseBox.add(Box.createVerticalStrut(20));
        baseBox.add(box2);
//        panelOfFesti.add(northPanel,BorderLayout.NORTH);
        panelOfFesti.add(baseBox,BorderLayout.CENTER);
        setMyFont(font,comboBoxOfFesti,showLabel,showAreaFesti,enterFesti);
    }

    public void setPanel(JPanel panel){
        this.allPanel=panel;
    }

    public void setView(InitWindow view){
        this.view=view;
    }

    public void setFirstPanel(){
        firstToMain = new JButton("返回");
        firstToMain.addActionListener(this);
        firstPanel = new JPanel();
        BorderLayout bord = new BorderLayout();
        firstPanel.setLayout(bord);
        solarButton = new JButton("查看二十四节气");
        festiButton = new JButton("查看节日");
        box1 = Box.createVerticalBox();
        box1.add(Box.createVerticalStrut(40));
        box1.add(solarButton);
        box2=Box.createVerticalBox();
        box2.add(Box.createVerticalStrut(40));
        box2.add(festiButton);
        baseBox = Box.createHorizontalBox();
        baseBox.add(Box.createHorizontalStrut(50));
        baseBox.add(box1);
        baseBox.add(Box.createHorizontalStrut(50));
        baseBox.add(box2);
        baseBox.add(Box.createHorizontalStrut(50));
        box1 = Box.createVerticalBox();
        box1.add(Box.createVerticalStrut(40));
        box1.add(firstToMain);
        baseBox.add(box1);
        baseBox.add(Box.createHorizontalStrut(50));
        firstPanel.add(baseBox,BorderLayout.CENTER);
    }
}

class OperateWindow implements ActionListener, ItemListener {
    CardLayout card = null;
    Box baseBox;
    Box box1;
    Box box2;
    Box box3;
    JPanel allPanel;
    JPanel viewPanel;
    JPanel addPanel;
    JPanel firstPanel;
    JPanel selectPanel;
    JButton viewButton;
    JButton addButton;
    JCheckBox[] checkArray;
    JButton enterOfView;
    JButton enterOfAdd;
    JButton viewBack;
    JButton addBack;
    JButton firstBack;
    JButton cleanButton;
    JComboBox<String> comboBox;
    JComboBox<String> comboBoxHour;
    JComboBox<String> comboBoxMinute;
    JTextArea inputArea;
    JLabel tipLabel;
    JLabel dateLabel;
    JLabel titleLabel;
    Font font01, font02;
    File file = null;
    FileReader read = null;
    BufferedReader Reader = null;
    FileWriter write = null;
    BufferedWriter Writer = null;
    InitWindow view;

    OperateWindow() {
        init();
        allPanel.setVisible(true);
        allPanel.setBounds(200, 200, 700, 500);
    }

    public void init() {
        font01 = new Font("微软雅黑", Font.PLAIN, 15);
        font02 = new Font("宋体", Font.BOLD, 20);
        checkArray = new JCheckBox[10];
        comboBoxHour = new JComboBox<>();
        comboBoxMinute = new JComboBox<>();
        for (int i = 0; i < 24; i++) {
            String hour = i + "";
            comboBoxHour.addItem(hour);
        }
        for (int i = 0; i < 60; i++) {
            String minute = i + "";
            comboBoxMinute.addItem(minute);
        }

        for (int i = 0; i < 10; i++) {
            checkArray[i] = new JCheckBox();
            checkArray[i].addItemListener(this);
        }

        createViewPanel();
        createAddPanel();
        createFirstPanel();

        allPanel = new JPanel();
        card = new CardLayout();
        allPanel.setLayout(card);
        allPanel.add("view", viewPanel);
        allPanel.add("add", addPanel);
        allPanel.add("first", firstPanel);
        card.show(allPanel, "first");
        setMyFont(font02, enterOfAdd, enterOfView, tipLabel, inputArea, addButton, viewButton, comboBoxHour, comboBoxMinute, dateLabel, firstBack);
        addButton.addActionListener(this);
        viewButton.addActionListener(this);
    }

    public void createViewPanel() {
        viewPanel = new JPanel();
        BorderLayout bord = new BorderLayout();
        viewPanel.setLayout(bord);
        tipLabel = new JLabel("您还有以下未完成的事项");
        enterOfView = new JButton("确定");
        viewBack = new JButton("返回");
        cleanButton = new JButton("清除");
        viewBack.addActionListener(this);
        enterOfView.addActionListener(this);
        cleanButton.addActionListener(this);
        tipLabel = new JLabel("您还有以下日程没完成");

        setMyFont(font02, tipLabel, cleanButton);
        showViewPanel();
    }

    public void setView(InitWindow view) {
        this.view = view;
    }

    public void createAddPanel() {
        addPanel = new JPanel();
        BorderLayout bord = new BorderLayout();
        addPanel.setLayout(bord);
        tipLabel = new JLabel("在未来一周内添加事件");
        inputArea = new JTextArea(5, 10);
        inputArea.requestFocus();
        comboBox = new JComboBox<>();
        enterOfAdd = new JButton("确定");
        enterOfAdd.addActionListener(this);
        addBack = new JButton("返回");
        addBack.addActionListener(this);
        LocalDate date = LocalDate.now();
        int count = 0;
        String str = null;
        setMyFont(font02, comboBox, tipLabel);
        setMyFont(font01, inputArea);
        while (count < 7) {
            int year = date.getYear();
            int month = date.getMonthValue();
            int Day = date.getDayOfMonth();
            str = year + "-" + month + "-" + Day;
            comboBox.addItem(str);
            date = date.plusDays(1);
            count++;
        }
        box1 = Box.createVerticalBox();
        box1.add(inputArea);
        box1.add(Box.createVerticalStrut(100));

        box2 = Box.createHorizontalBox();
        box2.add(addBack);
        box2.add(Box.createHorizontalStrut(10));
        dateLabel = new JLabel("选择日期");
        dateLabel.setFont(font02);
        box2.add(dateLabel);
        box2.add(Box.createHorizontalStrut(10));
        box2.add(comboBox);
        box2.add(Box.createHorizontalStrut(10));
        dateLabel = new JLabel("选择小时");
        dateLabel.setFont(font02);
        box2.add(dateLabel);
        box2.add(Box.createHorizontalStrut(10));
        box2.add(comboBoxHour);
        box2.add(Box.createHorizontalStrut(10));
        dateLabel = new JLabel("选择分钟");
        box2.add(dateLabel);
        box2.add(Box.createHorizontalStrut(10));
        box2.add(comboBoxMinute);

        addPanel.add(box2, BorderLayout.NORTH);
        addPanel.add(box1, BorderLayout.CENTER);
        addPanel.add(enterOfAdd, BorderLayout.SOUTH);
    }

    public void createFirstPanel() {
        firstBack = new JButton("返回");
        firstBack.addActionListener(this);
        firstPanel = new JPanel();
        BorderLayout bord = new BorderLayout();
        firstPanel.setLayout(bord);
        viewButton = new JButton("查看日程");
        addButton = new JButton("添加日程");
        box1 = Box.createVerticalBox();
        box1.add(Box.createVerticalStrut(40));
        box1.add(viewButton);
        box2 = Box.createVerticalBox();
        box2.add(Box.createVerticalStrut(40));
        box2.add(addButton);
        baseBox = Box.createHorizontalBox();
        baseBox.add(Box.createHorizontalStrut(50));
        baseBox.add(box1);
        baseBox.add(Box.createHorizontalStrut(50));
        baseBox.add(box2);
        baseBox.add(Box.createHorizontalStrut(50));
        box1 = Box.createVerticalBox();
        box1.add(firstBack);
        box1.add(Box.createVerticalStrut(80));
        baseBox.add(box1);
        baseBox.add(Box.createHorizontalStrut(50));
        firstPanel.add(baseBox, BorderLayout.CENTER);
    }

    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource() == enterOfAdd) {
            try {
                file = new File("ToDoList.txt");
                read = new FileReader(file);
                Reader = new BufferedReader(read);
                String str01 = null;
                int count = 0;
                while ((str01 = Reader.readLine()) != null) {
                    count++;
                }
                if (count == 10) {
                    JLabel warn = new JLabel("日程已经超出最大数!");
                    warn.setFont(font02);
                    JOptionPane.showMessageDialog(allPanel, warn, "超限提示框", JOptionPane.ERROR_MESSAGE);
                    inputArea.setText(null);
                    return;
                }
                Reader.close();
                read.close();

                int hour = Integer.parseInt(comboBoxHour.getSelectedItem().toString());
                int minute = Integer.parseInt(comboBoxMinute.getSelectedItem().toString());
                String hour02 = null;
                String minute02 = null;
                if (hour < 10) {
                    hour02 = "0" + hour;
                } else {
                    hour02 = hour + "";
                }
                if (minute < 10) {
                    minute02 = "0" + minute;
                } else {
                    minute02 = minute + "";
                }
                if (inputArea.getText().equals("")) {
                    JLabel warn = new JLabel("请至少输入一个字符");
                    warn.setFont(font02);
                    JOptionPane.showMessageDialog(this.allPanel, warn, "提醒输入框", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                String str02 = inputArea.getText().replace("\n", "");
                if (str02.length() == 0) {
                    JOptionPane.showMessageDialog(this.allPanel, "请至少输入一个字符", "提醒款", JOptionPane.ERROR_MESSAGE);
                    inputArea.setText(null);
                    inputArea.requestFocus();
                    return;
                }
                String mess = comboBox.getSelectedItem().toString() + "-" + hour02 + ":" + minute02 + str02;
                file = new File("ToDoList.txt");
                write = new FileWriter(file, true);
                Writer = new BufferedWriter(write);
                Writer.write(mess + "\n");
                Writer.close();
                write.close();
            } catch (IOException exp) {
            }
            inputArea.setText(null);
            inputArea.requestFocus();
            JLabel warn = new JLabel("创建日程成功!");
            warn.setFont(font02);
            JOptionPane.showMessageDialog(this.allPanel, warn, "提示框", JOptionPane.ERROR_MESSAGE);
        }
        if (ae.getSource() == viewButton) {
            card.show(allPanel, "view");
            showViewPanel();
        }
        if (ae.getSource() == addButton) {
            card.show(allPanel, "add");
        }
        if (ae.getSource() == viewBack) {
            card.show(allPanel, "first");
        }
        if (ae.getSource() == addBack) {
            card.show(allPanel, "first");
        }
        if (ae.getSource() == firstBack) {
            view.card.show(view.allPanel, "firstPanel");
        }
        if (ae.getSource() == cleanButton) {
            int n = JOptionPane.showConfirmDialog(this.allPanel, "确认全部清除?", "提醒框", JOptionPane.YES_NO_OPTION);
            if (n == JOptionPane.YES_OPTION) {
                file = new File("ToDoList.txt");
                try {
                    FileOutputStream out = new FileOutputStream(file);
                }
                catch (IOException exp) {}
                showViewPanel();
            }
        }
        if (ae.getSource() == enterOfView) {
            showViewPanel();
        }
    }

    public void setMyFont(Font font, JComponent... x) {
        for (JComponent c : x) {
            c.setFont(font);
        }
    }

    public void showViewPanel() {
        file = new File("ToDoList.txt");
        box2=Box.createVerticalBox();
//        if (file.length() == 0) {
//            JOptionPane.showMessageDialog(this.allPanel, "没有安排任何日程", "提醒框", JOptionPane.ERROR_MESSAGE);
//        }
        try {
            read = new FileReader(file);
            Reader = new BufferedReader(read);
            String str = null;
            int i = 0;
            String day01 = null;
            String day02 = null;
            while ((str = Reader.readLine()) != null) {
                day01 = str.substring(0, 10);
                if (day02 != null) {
                    if (!day02.equals(day01)) {
                        //如果上一条的日期和现在一条的日期不一样,则另外创建一个titlLabel;
                        titleLabel = new JLabel(day01);
                        titleLabel.setFont(font01);
                        box2.add(titleLabel);
                        box2.add(Box.createVerticalStrut(5));
                    }
                }
                if (day02 == null) {
                    titleLabel = new JLabel(day01);
                    titleLabel.setFont(font01);
                    box2.add(titleLabel);
                    box2.add(Box.createVerticalStrut(5));
                }
                day02 = day01;

                String rest = str.substring(10);
                String time = rest.substring(1, 6);  //几点几分
                String content = rest.substring(6); //代办事项
                checkArray[i] = new JCheckBox(content + "(" + time + ")");
                checkArray[i].setFont(font02);
                checkArray[i].addItemListener(this);
                box2.add(checkArray[i]);
                box2.add(Box.createVerticalStrut(1));
                i++;
            }
            Reader.close();
            read.close();

        } catch (IOException ioexp) {
        }
        viewPanel.removeAll();

        box1=Box.createHorizontalBox();
        box1.add(viewBack);
        box1.add(Box.createHorizontalStrut(20));
        tipLabel = new JLabel("您还有以下日程未完成:");
        tipLabel.setFont(font02);
        box1.add(tipLabel);

        baseBox = Box.createHorizontalBox();
        baseBox.add(Box.createHorizontalStrut(200));
        baseBox.add(enterOfView);
        baseBox.add(Box.createHorizontalStrut(20));
        baseBox.add(cleanButton);
        baseBox.add(Box.createHorizontalStrut(20));

        viewPanel.add(box1, BorderLayout.NORTH);
        viewPanel.add(box2,BorderLayout.CENTER);
        viewPanel.add(baseBox, BorderLayout.SOUTH);
    }

    public void itemStateChanged(ItemEvent it) {
        JCheckBox check = (JCheckBox) it.getSource();
        file = new File("ToDoList.txt");
        LinkedList<String> list = new LinkedList<>();
        try {
            read = new FileReader(file);
            Reader = new BufferedReader(read);
            String str = null;
            while ((str = Reader.readLine()) != null) {
                String regax = "[1234567890.:-]";
                String str1 = str.replaceAll(regax, "");
//                System.out.println(str1);
                if (!check.getText().contains(str1)) {
                    list.add(str);
                }
            }
            try {
                FileOutputStream out = new FileOutputStream(file);
            }
            catch (IOException ioe) {}
            Reader.close();
            read.close();
            write = new FileWriter(file);
            Writer = new BufferedWriter(write);
            Iterator<String> iter = list.iterator();
            while (iter.hasNext()) {
                String tar = iter.next();
//                System.out.println(tar);
                Writer.write(tar + "\n");
            }
            Writer.close();
            write.close();
        }
        catch (IOException exp) {}
    }
}


上一篇:CSS3伪元素选择器 ::before ::after | 记录自己的前端学习日子


下一篇:C++教程