java实现页面显示日历,java 日历控件
本文实例为大家分享了爪哇实现窗体程序显示日历的具体代码,供大家参考,具体内容如下
实训要求:
代码:
测试类:
导入Java。awt。*;导入Java。awt。事件。*;导入javax。挥棒。*;公共类测试扩展JFrame { JButton week1,week2,week3,week4,week5,week6,week7,next,procalenda bean CB=new calenda bean();JLabel[]标签;JLabel now公共静态void main(String[]args){测试帧=新测试();frame.setBounds(650,300,550,550);框架。setdefaultcloseoperation(JFrame .EXIT _ ON _ CLOSE);frame.setTitle(日历);框架。设置可见(真);}公测(){ int year,months et layout(new BorderLayout());JPanel p north=new JPanel();CB=new calenda bean();CB。setyear(2017年);CB。设定月份(11);string[]a=CB。获取日历();next=new JButton(上月);pro=new JButton(下月);下一个。setactioncommand(“上个月”);亲。setactioncommand(下月);下一个。addactionlistener(new action listener(){ public void action performed(action event e){ CB。采取的行动(e);} });亲。addactionlistener(new action listener(){ public void action performed(action event e){ CB。采取的行动(e);} });pNorth.add(下一个);p北。添加(pro);add(pNorth,BorderLayout .北);GridLayout grid=new GridLayout(8,7);JPanel pCenter=new JPanel();第1周=新JButton(日);第2周=新JButton(一);第3周=新JButton(二);第4周=新JButton(三);第5周=新JButton(四);第6周=新JButton(五);第7周=新JButton(六);pCenter.add(第一周);pCenter.add(第二周);pCenter.add(第三周);pCenter.add(第4周);pCenter.add(第5周);pCenter.add(第6周);pCenter.add(第7周);label=new JLabel[42];for(int I=0;I 42I){ label[I]=new JLabel();pcenter。添加(标签[I]);} CB。标签=这个。标签;
for (int i = 0; i < a.length; i++) { if (i % 7 == 0) { label[i].setText(""); } label[i].setText(" "+a[i]); } pCenter.setLayout(grid); add(pCenter, BorderLayout.CENTER); JPanel pSouth = new JPanel(); now = new JLabel(); now.setText("日历:" + cb.year + "年" + cb.month + "月"); cb.now = now; pSouth.add(now); add(pSouth, BorderLayout.SOUTH); } } CalendaBean类:
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Calendar; import javax.swing.*; public class CalendaBean implements ActionListener { JLabel[] label; JLabel now; String[] day; int year = 0, month = 0; public void setYear(int year) { this.year = year; } public void setMonth(int month) { this.month = month; } public void actionPerformed(ActionEvent e) { String str = e.getActionCommand(); if (str.equals("lastmonth")) { month--; if (month == 0) { month = 12; year--; } } else if (str.equals("nextmonth")) { month++; if (month == 13) { month = 1; year++; } } now.setText("日历:" + year + "年" + month + "月"); String[] a = getCalendar(); for (int i = 0; i < a.length; i++) { if (i % 7 == 0) { label[i].setText(""); } label[i].setText(" "+a[i]); } } public String[] getCalendar() { String[] a = new String[42]; Calendar rili = Calendar.getInstance(); rili.set(year, month - 1, 1); int weekDay = rili.get(Calendar.DAY_OF_WEEK) - 1; int day = 0; if (month == 1 month == 3 month == 5 month == 7 month == 8 month == 10 month == 12) { day = 31; } if (month == 4 month == 6 month == 9 month == 11) { day = 30; } 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; } }
运行结果:
小结:
学习了Calendar类,其他的,界面的话顺着来就好。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持盛行IT。
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。