文章标题

来源:互联网 发布:金融it程序员 编辑:程序博客网 时间:2024/06/05 21:56

自己做的一个简单日历

public class Main{    public static void main(String[] args)    {         MyDate md=new MyDate(2015,1);         MyFrame mf=new MyFrame();         mf.setFrame(md);    }}
public class MyDate{    private int m;    private int y;    SimpleDateFormat sdf;    Calendar c = Calendar.getInstance();    String strEE;    public MyDate (int y, int m)    //设置当前时间,Calendar中月份从0开始,0代表一月份,传进来的值减1才是代表要设置的月份    {        c.set(y, m - 1, 01);        this.y = y;        this.m = m;    }    public void dc()    {        strEE = String.format("%tA", c.getTime());        System.out.println(strEE);    }    public int dayofmonth()//返回当月有多天    {        if (m == 4 || m == 6 || m == 9 || m == 11)        {            return 30;        }        else if (m == 2)        {            if (y % 4 == 0 && y%100 != 0 || y % 400 == 0)            {                return 29;            }            else            {                return 28;            }        }        else        {            return 31;        }    }    public String weekofmonth()//  返回当月第一天是星期几    {        return String.format("%tA", c.getTime());    }}
public class MyFrame implements ActionListener{    private MyThread mt;    MyDate md;    JLabel[] jlabelArray = new JLabel[7];    JButton[] jbuttonArray = new JButton[42];    JPanel jp1 = new JPanel();    JPanel jp2 = new JPanel();    int k = 0;    String[] str = {"日", "一", "二", "三", "四", "五", "六"};    JTextField jtf1=new JTextField(5);;    JTextField jtf2=new JTextField(5);      public void setFrame(MyDate md)    {        this.md=md;        JFrame f = new JFrame();        mt = new MyThread(f);        mt.start();        for (int i = 0; i < jlabelArray.length; i++)        {            jlabelArray[i] = new JLabel("星期" + str[i]);            jp1.add(jlabelArray[i]);        }//设置星期面板        for (int j = 0; j < jlabelArray.length; j++)        {            if (md.weekofmonth().equals("星期" + str[j]))            {                k = j;            }        }//判断dc.weekofmonth为星期几,若为5,则从星期五设置当月1号        for (int i = 0; i < jbuttonArray.length; i++)        {            if (i > k && i < md.dayofmonth() + k)                //从K+1处设置2号,后面依次设置当前月的天数(dc.dayofmonth())个button            {                jbuttonArray[i] = new JButton(String.valueOf((i + 1 - k)));                jp2.add(jbuttonArray[i]);            }            else if (i == k)//从第K个button设置1号            {                jbuttonArray[i] = new JButton(String.valueOf(1));            }            else//其余的button设置为空            {                jbuttonArray[i] = new JButton(" ");            }            jp2.add(jbuttonArray[i]);            jbuttonArray[i].setBackground(Color.yellow);        }        JLabel jl1=new JLabel("请输入年份:");        JLabel jl2=new JLabel("请输入月份:");        JPanel jp=new JPanel();        JButton jb1=new JButton("确定");        jb1.addActionListener(this);        jp.add(jl1);        jp.add(jtf1);        jp.add(jl2);        jp.add(jtf2);        jp.add(jb1);        jp1.setLayout(new GridLayout(1, 7));        jp2.setLayout(new GridLayout(6, 7));        jp1.setBounds(0, 0, 500, 80);        jp2.setBounds(0, 80, 500, 220);        f.add(jp1, BorderLayout.NORTH);        f.add(jp2);        f.add(jp,BorderLayout.SOUTH);        f.setSize(600, 400);        f.setLocation(300, 300);        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        f.setVisible(true);    }    @Override    public void actionPerformed(ActionEvent e) {        MyDate md1=new MyDate(Integer.valueOf(jtf1.getText()),Integer.valueOf(jtf2.getText()));            for (int j = 0; j < jlabelArray.length; j++)            {                if (md1.weekofmonth().equals("星期" + str[j]))                {                    k = j;                }            }//判断dc.weekofmonth为星期几,若为5,则从星期五设置当月1号            for (int i = 0; i < jbuttonArray.length; i++)            {                if (i > k && i < md1.dayofmonth() + k)                    //从K+1处设置2号,后面依次设置当前月的天数(dc.dayofmonth())个button                {                    jbuttonArray[i].setText(String.valueOf((i + 1 - k)));//                  jp2.add(jbuttonArray[i]);                }                else if (i == k)//从第K个button设置1号                {                    jbuttonArray[i].setText(String.valueOf(1));                }                else//其余的button设置为空                {                    jbuttonArray[i].setText(" ");                }                jbuttonArray[i].setBackground(Color.yellow);//              jp2.add(jbuttonArray[i]);            }    }}
public class MyThread extends Thread//用线程设置当前时间,放在JFrame的标题栏{    private JFrame jf;    public MyThread(JFrame jf)    {        this.jf = jf;    }     public void run()    {        while (true)        {            Date date = new Date();            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");            String sdate = sdf.format(date);            jf.setTitle("当前时间为:" + sdate);            try            {                Thread.sleep(1000);            }            catch (InterruptedException e)            {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }}

0 0