• gui小日历


    package MyCal;
    
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.BorderLayout;
    import javax.swing.JSpinner;
    import javax.swing.JComboBox;
    import javax.swing.DefaultComboBoxModel;
    import javax.swing.JButton;
    import javax.swing.SpinnerNumberModel;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.util.Calendar;
    
    import javax.swing.JLabel;
    import java.awt.Color;
    
    public class MyCal {
    
        private JFrame frame;
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        MyCal window = new MyCal();
                        window.frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the application.
         */
        public MyCal() {
            initialize();
        }
    
        /**
         * Initialize the contents of the frame.
         */
        private void initialize() {
            frame = new JFrame();//基本窗口框架
            frame.setBounds(100, 100, 569, 417);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            JPanel panel1 = new JPanel();//上面的面板
            frame.getContentPane().add(panel1, BorderLayout.NORTH);
            
            JSpinner spinner = new JSpinner();//年份微调
            spinner.setModel(new SpinnerNumberModel(2016, 1970, 2100, 1));
            panel1.add(spinner);
            
            JComboBox comboBox = new JComboBox();//月份微调
            comboBox.setModel(new DefaultComboBoxModel(new String[] {"u4E00u6708", "u4E8Cu6708", "u4E09u6708", "u56DBu6708", "u4E94u6708", "u516Du6708", "u4E03u6708", "u516Bu6708", "u4E5Du6708", "u5341u6708", "u5341u4E00u6708", "u5341u4E8Cu6708"}));
            comboBox.setMaximumRowCount(12);
            panel1.add(comboBox);
            
            JPanel panel3 = new JPanel();//下面面板
            frame.getContentPane().add(panel3, BorderLayout.SOUTH);
            
            JLabel label1 = new JLabel("选择一个年份或月份日历开始启动");//添加label说明
            label1.setForeground(Color.RED);
            panel3.add(label1);
            
            JPanel panel2 = new JPanel();//中间面板,主面板
            frame.getContentPane().add(panel2, BorderLayout.CENTER);
            panel2.setLayout(new GridLayout(7, 7, 0, 0));
            
            ItemPolice itemPolice1=new ItemPolice();//new一个监视器
            itemPolice1.setCal(spinner, comboBox,panel2,label1);//组合
            itemPolice1.show();//显示开始日历
            comboBox.addItemListener(itemPolice1);//下拉项添加监视器
            spinner.addChangeListener(itemPolice1);//微调添加监视器
        }
    }
    
    class CalendarHuang{
        int year,month;
        void setYearAndMonth(int a,int b){
            year=a;
            month=b;
        }
        String[] getCalendar(){
            String []a=new String[42];
            Calendar Calender1=Calendar.getInstance();
            Calender1.set(year, month-1,1);
            int weekday=Calender1.get(Calender1.DAY_OF_WEEK)-1;
    //        System.out.println(month);
            int day = 0;
            if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
                day=31;
            }else if(month==4||month==6||month==9|month==11)
                day=30;
            else if(month==2){
                if(year%4==0&&year%100!=0||year%400==0)
                    day=29;
                else 
                    day=28;
            }
            for(int i=0;i<weekday;i++)
                a[i]=" ";
            for(int i=weekday,n=1;i<weekday+day;i++){
                a[i]=String.valueOf(n);
                n++;
            }
            for(int i=weekday+day;i<a.length;i++){
                a[i]=" ";
            }
            return a;
        }
    }
    
    class ItemPolice implements ItemListener,ChangeListener,ActionListener{//实现ItemListener接口的类
        JSpinner combo1;//微调项
        JComboBox combo2;//下拉项
        JPanel panel2;//主面板
        JLabel label1;//说明标签
        String day[]=new String[42];//天数
        CalendarHuang cal1=new CalendarHuang();//日历类
        int year=2016,month=1;//开始值
        void show(){//开始先有一个月份
            String s1[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
            for(int i=0;i<7;i++){
                panel2.add(new JLabel(s1[i]));
            }
            cal1.setYearAndMonth(year, month);
            day=cal1.getCalendar();
            for(int i=0;i<day.length;i++){
                panel2.add(new JButton(day[i]));
            }
        }
        void setCal(JSpinner combox1,JComboBox combox2,JPanel panel2,JLabel label1){//组合
            this.combo1=combox1;
            this.combo2=combox2;
            this.panel2=panel2;
            this.label1=label1;
        }
        public void itemStateChanged(ItemEvent e){//下拉事件
            panel2.removeAll();  
            panel2.revalidate();
            panel2.getParent().repaint();
            month=combo2.getSelectedIndex()+1;
            String s1[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
            for(int i=0;i<7;i++){
                panel2.add(new JLabel(s1[i]));
            }
            cal1.setYearAndMonth(year, month);
            day=cal1.getCalendar();
            for(int i=0;i<day.length;i++){
                JButton button2=new JButton(day[i]);
                button2.addActionListener(this);
                panel2.add(button2);
            }
        }
        public void stateChanged(ChangeEvent e) {//微调事件
            panel2.removeAll();  
            panel2.revalidate();
            panel2.getParent().repaint();
            year=Integer.parseInt(combo1.getValue().toString());
            String s1[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
            for(int i=0;i<7;i++){
                panel2.add(new JLabel(s1[i]));
            }
            cal1.setYearAndMonth(year, month);
            day=cal1.getCalendar();
            for(int i=0;i<day.length;i++){
                JButton button2=new JButton(day[i]);
                button2.addActionListener(this);
                panel2.add(button2);
            }
        }
        public void actionPerformed(ActionEvent e) {//按钮事件
            String s=e.getActionCommand();
            label1.setText("日期是:"+year+" 年 "+month+" 月 "+s+" 日");
        }
    }
  • 相关阅读:
    HDU5446 Unknown Treasure
    C语言课设——通讯录操作系统
    判断设备是否横屏
    禁止用户横屏
    用javascript编写ps脚本
    用户鼠标指针的样式
    如何使用typescript开发node(自动编译,自动重启)
    CSS3:box-sizing:不再为匣子模型而烦恼
    命令行开启自动编译scss(webstrom可开启自动编译)
    TypeError: Cannot read property 'replace' of undefined ----angular2
  • 原文地址:https://www.cnblogs.com/vhyc/p/6017352.html
Copyright © 2020-2023  润新知