java大作业 模拟win7日期时间属性

package Jamian;

import java.awt.Button;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Label;
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.JPanel;
import javax.swing.JSplitPane;


@SuppressWarnings("serial")
public class MyCalendar extends JFrame{
    Thread timer=null;//更新UI的线程,每一秒钟重绘一次时钟
    public MyCalendar() {
        setTitle("简易时钟日历");
        setSize(400,250);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);

        JSplitPane splitPane=new JSplitPane();//将Jframe分割成左右两部分,左边实现日历,右边实现时钟
        splitPane.setBackground(Color.orange);
        splitPane.setDividerLocation(250);
        splitPane.setDividerSize(-1);

        ClockPanel clockPanel=new ClockPanel();//右侧面板的时钟
        CalendarPanel calendarPanel=new CalendarPanel(this);//左侧面板的日历
        splitPane.setRightComponent(clockPanel);
        splitPane.setLeftComponent(calendarPanel);
        add(splitPane);
        setVisible(true);

        new Thread(clockPanel).start();
    }
    public static void main(String[] args) {
         new MyCalendar();
    }
}
/**
 * JFrame左侧面板实现日历功能
 **/


@SuppressWarnings("serial")
//*****************************************************************
class CalendarPanel extends JPanel implements ActionListener{
    JPanel selectPanel; //主面板上方三个按钮,分别为月份减少按钮,选择月份、年份按钮,月份增加按钮
    JPanel showPanel; //该面板有三种显示状态,一种即显示日历,一种显示选择月份,一种显示选择年份
    JPanel weekdayPanel; //该面板被添加打showPanel,显示静态文本周一到周日
    JPanel calshowPanel; //该面板被添加到shouPannel,显示一个月中的每天
    JPanel selectmonth; //该面板被添加到showPanel,显示选择月份的按钮
    JPanel selectyear; //该面板被添加到shouPanel,显示选择年份的按钮
    JButton btleft;
    JButton btright;
    JButton btmid;
    Button btday[]=new Button[42];
    Button btmonth[]=new Button[12];
    Button btyear[]=new Button[12];
    Calendar curcalendar; //记录当前日历上显示的日期
    JFrame jFrame; //存储Parent的JFrame,用于调用其setVisible更新界面(由于未实现paint函数,所以用此函数可以更新)

   
    public CalendarPanel(JFrame jFrame) {       
        this.jFrame=jFrame;
        setLayout(null);
        setBackground(new Color(186, 231, 241));
        selectPanel =new JPanel(null);
        selectPanel.setBackground(Color.darkGray);
        selectPanel.setBounds(0, 0, 250, 35);

        btleft=new JButton("<");
        btleft.setBounds(20, 5, 45, 25);
        btleft.addActionListener(this);
        selectPanel.add(btleft);

        btmid=new JButton("  ");
        btmid.setBounds(70,5,120,25);
        btmid.addActionListener(this);
        selectPanel.add(btmid);

        btright=new JButton(">");
        btright.setBounds(195, 5, 45, 25);
        btright.addActionListener(this);
        selectPanel.add(btright);
        add(selectPanel);

        weekdayPanel=new JPanel();
        weekdayPanel.setBounds(0, 35, 250, 30);
        weekdayPanel.setBackground(Color.gray);
        weekdayPanel.setLayout(new GridLayout(1,7));
        weekdayPanel.add(new Label("日"));
        weekdayPanel.add(new Label("一"));
        weekdayPanel.add(new Label("二"));
        weekdayPanel.add(new Label("三"));
        weekdayPanel.add(new Label("四"));
        weekdayPanel.add(new Label("五"));
        weekdayPanel.add(new Label("六"));

        calshowPanel=new JPanel();
        calshowPanel.setBounds(0, 65, 250, 155);
        calshowPanel.setLayout(new GridLayout(6, 7));

        for(int i=0;i<42;i++){
            btday[i]=new Button("");
            btday[i].addActionListener(this);
        }

        curcalendar=Calendar.getInstance();
        int monthday=curcalendar.get(Calendar.DAY_OF_MONTH);
        int weekday=curcalendar.get(Calendar.DAY_OF_WEEK);
        int startcol=7-(monthday-weekday)%7;
        if(startcol==0) startcol=7;

        Calendar lastcalendar=(Calendar) curcalendar.clone();
        lastcalendar.set(Calendar.DAY_OF_MONTH, 1);
        lastcalendar.add(Calendar.DAY_OF_MONTH, -startcol);
        int tablestart=lastcalendar.get(Calendar.DAY_OF_MONTH);

        String midbt_lab=curcalendar.get(Calendar.YEAR)+"年"+(curcalendar.get(Calendar.MONTH)+1)+"月";
        btmid.setText(midbt_lab);

        int k=0;
        for(int i=0;i<startcol;i++){
            btday[k].setForeground(Color.gray);
            btday[k].setLabel((tablestart++)+"");
            calshowPanel.add(btday[k++]);
        }
        for(int i=1;i<=curcalendar.getActualMaximum(Calendar.DAY_OF_MONTH);i++){
            btday[k].setForeground(Color.black);
            btday[k].setLabel(i+"");
            calshowPanel.add(btday[k++]);
        }
        for(int i=1;i<=42-startcol-curcalendar.getActualMaximum(Calendar.DAY_OF_MONTH);i++){
            btday[k].setForeground(Color.gray);
            btday[k].setLabel(i+"");
            calshowPanel.add(btday[k++]);
        }

        showPanel=new JPanel(null);
        showPanel.setBounds(0,0,250,220);
        showPanel.add(weekdayPanel);
        showPanel.add(calshowPanel);

        add(showPanel);

        selectmonth=new JPanel(new GridLayout(3, 4));
        selectmonth.setBounds(0, 35, 250, 185);
        for(int i=0;i<12;i++){
            btmonth[i]=new Button((i+1)+"月");
            btmonth[i].addActionListener(this);
            selectmonth.add(btmonth[i] );
        }

        selectyear=new JPanel(new GridLayout(3, 4));
        selectyear.setBounds(0,35,250,185);
        for(int i=0;i<12;i++){
            btyear[i]=new Button((curcalendar.get(Calendar.YEAR)-6+i)+"");
            btyear[i].addActionListener(this);
            selectyear.add(btyear[i] );
        }
    }
    
    //***********************************************************************
    public void updatebtday(){
        int monthday=curcalendar.get(Calendar.DAY_OF_MONTH);
        int weekday=curcalendar.get(Calendar.DAY_OF_WEEK);
        int startcol=7-(monthday-weekday)%7;
        if(startcol==0) startcol=7;

        Calendar lastcalendar=(Calendar) curcalendar.clone();
        lastcalendar.set(Calendar.DAY_OF_MONTH, 1);
        lastcalendar.add(Calendar.DAY_OF_MONTH, -startcol);
        int tablestart=lastcalendar.get(Calendar.DAY_OF_MONTH);

        String midbt_lab=curcalendar.get(Calendar.YEAR)+"年"+(curcalendar.get(Calendar.MONTH)+1)+"月";
        btmid.setText(midbt_lab);

        int k=0;
        for(int i=0;i<startcol;i++){
            btday[k].setForeground(Color.gray);
            btday[k++].setLabel((tablestart++)+"");
        }
        for(int i=1;i<=curcalendar.getActualMaximum(Calendar.DAY_OF_MONTH);i++){
            btday[k].setForeground(Color.black);
            btday[k++].setLabel(i+"");
        }
        for(int i=1;i<=42-startcol-curcalendar.getActualMaximum(Calendar.DAY_OF_MONTH);i++){
            btday[k].setForeground(Color.gray);
            btday[k++].setLabel(i+"");
        }
    }
    
    //**************************************************************
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==btmid){
            JPanel getpanel=(JPanel) showPanel.getComponent(0);
            if(getpanel==weekdayPanel){
                btmid.setText(curcalendar.get(Calendar.YEAR)+"");
                showPanel.removeAll();
                showPanel.add(selectmonth);
            }else if(getpanel==selectmonth){
                showPanel.removeAll();
                showPanel.add(selectyear);
                btmid.setText((curcalendar.get(Calendar.YEAR)-6)+"-"+(curcalendar.get(Calendar.YEAR)+5));
            }

            jFrame.setVisible(true);
        }else if(e.getSource()==btleft){
            JPanel getpanel=(JPanel) showPanel.getComponent(0);
            if(getpanel==weekdayPanel){
                int month1=curcalendar.get(Calendar.MONTH);
                curcalendar.set(Calendar.MONTH, month1-1);
                updatebtday();
            }else if(getpanel==selectmonth){
                int year=curcalendar.get(Calendar.YEAR);
                curcalendar.set(Calendar.YEAR, year-1);
                btmid.setText(curcalendar.get(Calendar.YEAR)+"");
            }else{
                String str=btyear[0].getLabel();
                int first=Integer.parseInt(str);
                for(int i=11;i>=0;i--){
                    btyear[i].setLabel((--first)+"");
                }
                btmid.setText(first+"-"+(first+11));
            }

        }else if(e.getSource()==btright){
            JPanel getpanel=(JPanel) showPanel.getComponent(0);
            if(getpanel==weekdayPanel){
                int month1=curcalendar.get(Calendar.MONTH);
                curcalendar.set(Calendar.MONTH, month1+1);
                updatebtday();
            }else if(getpanel==selectmonth){
                int year=curcalendar.get(Calendar.YEAR);
                curcalendar.set(Calendar.YEAR, year+1);
                btmid.setText(curcalendar.get(Calendar.YEAR)+"");
            }else{
                String str=btyear[11].getLabel();
                int last=Integer.parseInt(str);
                for(int i=0;i<12;i++){
                    btyear[i].setLabel((++last)+"");
                }
                btmid.setText((last-11)+"-"+last);
            }
        }else if(((Button) e.getSource()).getForeground()==Color.gray ){
            String command=e.getActionCommand();
            int com_int=Integer.parseInt(command);
            if(com_int>20){
                int month3=curcalendar.get(Calendar.MONTH);
                curcalendar.set(Calendar.MONTH, month3-1);
            }else if( com_int<20){
                int month4=curcalendar.get(Calendar.MONTH);
                curcalendar.set(Calendar.MONTH, month4+1);
            }
            updatebtday();
        }else if(((Button)e.getSource()).getForeground()==Color.black){

        }else {
            switch(e.getActionCommand()){
            case "1月":
                curcalendar.set(Calendar.MONTH,0);
                break;
            case "2月":
                curcalendar.set(Calendar.MONTH,1);
                break;
            case "3月":
                curcalendar.set(Calendar.MONTH,2);
                break;
            case "4月":
                curcalendar.set(Calendar.MONTH,3);
                break;
            case "5月":
                curcalendar.set(Calendar.MONTH,4);
                break;
            case "6月":
                curcalendar.set(Calendar.MONTH,5);
                break;
            case "7月":
                curcalendar.set(Calendar.MONTH,6);
            break;
            case "8月":
                curcalendar.set(Calendar.MONTH,7);
                break;
            case "9月":
                curcalendar.set(Calendar.MONTH,8);
                break;
            case "10月":
                curcalendar.set(Calendar.MONTH,9);
                break;
            case "11月":
                curcalendar.set(Calendar.MONTH,10);
                break;
            case "12月":
                curcalendar.set(Calendar.MONTH,11);
                break;  
            default:
                String string=e.getActionCommand();
                int year=Integer.parseInt(string);
                curcalendar.set(Calendar.YEAR, year);
                btmid.setText(curcalendar.get(Calendar.YEAR)+"");
                showPanel.removeAll();
                showPanel.add(selectmonth);
                return;
            }//switch
            showPanel.removeAll();
            updatebtday();
            showPanel.add(weekdayPanel);
            showPanel.add(calshowPanel);
            jFrame.setVisible(true);
        }
    }
}


@SuppressWarnings("serial")
class ClockPanel extends JPanel implements Runnable{
    int lastxs=50, lastys=30, lastxm=50, lastym=30, lastxh=50, lastyh=30;
    Label cur_day;
    Label cur_clock;
    Label cur_week;
    public ClockPanel() {
        setLayout(null);
        setBackground(Color.white);
        cur_day=new Label("");
        cur_day.setBounds(40, 5, 67, 20);
        cur_day.setBackground(new Color(186, 231, 241));
        add(cur_day);

        cur_clock=new Label("");
        cur_clock.setBounds(50, 160, 55, 20);
        cur_clock.setBackground(new Color(186, 231, 241));
        add(cur_clock);

        cur_week=new Label("");
        cur_week.setBounds(56, 180, 40, 20);
        cur_week.setBackground(new Color(186, 231, 241));
        add(cur_week);
    }
    
    
    @Override
    public void paint(Graphics g) {
        g.setColor(new Color(186, 231, 241));
        g.fillRect(0, 0, 150, 250);
        int xh, yh, xm, ym, xs, ys, s, m, h, xcenter, ycenter;
        Date currenttime = new Date(); //获取当前日期和时间
        s = currenttime.getSeconds();
        m = currenttime.getMinutes();
        h = currenttime.getHours();
        SimpleDateFormat formatter_day= new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatter_clock = new SimpleDateFormat("HH:mm:ss");
        SimpleDateFormat formatter_week = new SimpleDateFormat("EEEE");
        cur_day.setText(formatter_day.format(currenttime));
        cur_clock.setText(formatter_clock.format(currenttime));
        cur_week.setText(formatter_week.format(currenttime));
        xcenter=75; ycenter=90; //图形钟的原点
        //以下计算秒针、分针、时针位置
        xs = (int)(Math.cos(s * 3.14f/30 - 3.14f/2) * 45 + xcenter);
        ys = (int)(Math.sin(s * 3.14f/30 - 3.14f/2) * 45 + ycenter);
        xm = (int)(Math.cos(m * 3.14f/30 - 3.14f/2) * 40 + xcenter);
        ym = (int)(Math.sin(m * 3.14f/30 - 3.14f/2) * 40 + ycenter);
        xh = (int)(Math.cos((h*30+m/2)*3.14f/180-3.14f/2)*30+xcenter);
        yh = (int)(Math.sin((h*30+m/2)*3.14f/180-3.14f/2)*30+ycenter);
        g.setFont(new Font("TimesRoman", Font.PLAIN, 14));
        g.setColor(Color.lightGray); //设置表盘颜色
        g.fillOval(xcenter-60, ycenter-58, 120, 120);
        g.setColor(Color.darkGray); //设置表盘数字颜色
        g.drawString("9",xcenter-53,ycenter+5); //画表盘上的数字
        g.drawString("3",xcenter+45,ycenter+5);
        g.drawString("12",xcenter-8,ycenter-41);
        g.drawString("6",xcenter-3,ycenter+55);

        g.setColor(Color.gray); //使用红色画新指针
        g.drawLine(xcenter, ycenter, xs, ys);
        g.drawLine(xcenter, ycenter-2, xm, ym);
        g.drawLine(xcenter-2, ycenter, xm, ym);
        g.drawLine(xcenter, ycenter-2, xh, yh);
        g.drawLine(xcenter-2, ycenter, xh, yh);
        lastxs=xs; lastys=ys; //保存指针位置
        lastxm=xm; lastym=ym;
        lastxh=xh; lastyh=yh; 
    }
    //***************************************************************************
    @Override
    public void run() {
        while (true)        {
            try {
                Thread.sleep(1000);
            }catch (InterruptedException e) {}
            repaint(); 
        }
    }
	
}
posted @ 2021-01-23 22:03  索饮  阅读(140)  评论(0编辑  收藏  举报