9、万年历要求:使用图形用户界面。实现日期与星期的查询importjava.awt.*;importjava.awt.event.*;importjava.text.*;importjava.util.*;importjavax.swing.*;importjavax.swing.table.*;publicclasstest1extendsJFrameimplementsActionListener{privateCalendarcal=Calendar.getInstance();privateString[]str={星期一,星期二,星期三,星期四,星期五,星期六,星期日};privateDefaultTableModeldtm=newDefaultTableModel(null,str);privateJTabletable=newJTable(dtm);//装日期的表格privateJScrollPanesp=newJScrollPane(table);privateJButtonbLastYear=newJButton(上一年);privateJButtonbNextYear=newJButton(下一年);privateJButtonbLastMonth=newJButton(上月);privateJButtonbNextMonth=newJButton(下月);privateJTextFieldYear=newJTextField(4);//jtfYear年份显示和输入文本privateJLabeljla=newJLabel(年);privateJTextFieldmonth=newJTextField(4);privateJLabeljla1=newJLabel(月);privateJTextFieldjtfMonth=newJTextField(2);//jtfMonth月份显示文本框privateJPanelp1=newJPanel();//装入控制日期按钮的模块privateJPanelp2=newJPanel();privateJPanelp3=newJPanel(newBorderLayout());privateJPanelp4=newJPanel(newGridLayout(2,1));privateJPanelp5=newJPanel(newBorderLayout());privateJLabell=newJLabel(文本框中可直接键入要查找的年份,以提高查询效率);privateJLabellt=newJLabel();privateintlastTime;publictest1(){super(万年历);//框架命名this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口关闭函数this.getContentPane().setLayout(newBorderLayout(10,0));table.setGridColor(Color.GRAY);//星期之间的网格线是灰色的table.setColumnSelectionAllowed(true);table.setSelectionBackground(Color.BLACK);//当选定某一天时这一天背景黑色table.setSelectionForeground(Color.GREEN);//选定的日期字体是绿色的table.setBackground(newColor(184,207,229));//日期显示表格颜色浅蓝色table.setFont(newFont(黑体,Font.BOLD,24));//日期数字字体格式table.setRowHeight(30);//表格的高度Year.addActionListener(this);//可输入年份的文本框month.addActionListener(this);//可输入年份的文本框//为各个按钮添加监听函数bLastYear.addActionListener(this);bNextYear.addActionListener(this);bLastMonth.addActionListener(this);bNextMonth.addActionListener(this);//将按钮添加到Jpanel上p1.add(bLastYear);p1.add(Year);//年份输入文本框p1.add(jla);p1.add(month);//年份输入文本框p1.add(jla1);p1.add(bNextYear);p1.add(bLastMonth);p1.add(jtfMonth);p1.add(bNextMonth);p3.add(p2,BorderLayout.SOUTH);p4.add(l);p4.add(lt);p5.add(p4,BorderLayout.SOUTH);p5.add(sp,BorderLayout.CENTER);p5.add(p1,BorderLayout.NORTH);this.getContentPane().add(p5,BorderLayout.CENTER);this.getContentPane().add(p3,BorderLayout.EAST);String[]strDate=DateFormat.getDateInstance().format(newDate()).split(-);//获得日期cal.set(Integer.parseInt(strDate[0]),Integer.parseInt(strDate[1])-1,0);showCalendar(Integer.parseInt(strDate[0]),Integer.parseInt(strDate[1]),cal);jtfMonth.setEditable(false);//设置月份的文本框为不可编辑Year.setText(strDate[0]);month.setText(strDate[1]);jtfMonth.setText(strDate[1]);newTimer(lt).start();this.setBounds(200,200,600,320);this.setResizable(false);this.setVisible(true);}publicvoidshowCalendar(intlocalYear,intlocalMonth,Calendarcld){intDays=getDaysOfMonth(localYear,localMonth)+cld.get(Calendar.DAY_OF_WEEK)-2;Object[]ai=newObject[7];lastTime=0;for(inti=cld.get(Calendar.DAY_OF_WEEK)-1;i=Days;i++){ai[i%7]=String.valueOf(i-(cld.get(Calendar.DAY_OF_WEEK)-2));if(i%7==6){dtm.addRow(ai);ai=newObject[7];lastTime++;}}dtm.addRow(ai);}publicintgetDaysOfMonth(intyear,intMonth)//显示所选月份的天数{if(Month==1||Month==3||Month==5||Month==7||Month==8||Month==10||Month==12){return31;}if(Month==4||Month==6||Month==9||Month==11){return30;}if(year%4==0&&year%100!=0||year%400==0)//闰年{return29;}else{return28;}}publicvoidactionPerformed(ActionEvente){if(e.getSource()==Year||e.getSource()==bLastYear||e.getSource()==bNextYear||e.getSource()==bLastMonth||e.getSource()==bNextMonth){intm,y;try//控制输入的年份正确,异常控制{if(Year.getText().length()!=4){thrownewNumberFormatException();}y=Integer.parseInt(Year.getText());m=Integer.parseInt(jtfMonth.getText());}catch(NumberFormatExceptionex){JOptionPane.showMessageDialog(this,请输入4位0-9的数字!,年份有误,JOptionPane.ERROR_MESSAGE);return;}for(inti=0;ilastTime+1;i++){dtm.removeRow(0);}if(e.getSource()==bLastYear){Year.setText(String.valueOf(--y));}if(e.getSource()==bNextYear){Year.setText(String.valueOf(++y));}if(e.getSource()==bLastMonth){if(m==1){Year.setText(String.valueOf(--y));m=12;jtfMonth.setText(String.valueOf(m));}else{jtfMonth.setText(String.valueOf(--m));}}if(e.getSource()==bNextMonth){if(m==12){Year.setText(String.valueOf(++y));m=1;jtfMonth.setText(String.valueOf(m));}else{jtfMonth.setText(String.valueOf(++m));}}cal.set(y,m-1,0);showCalendar(y,m,cal);month.setText(String.valueOf(m));}}publicstaticvoidmain(String[]args){JFrame.setDefaultLookAndFeelDecorated(true);JDialog.setDefaultLookAndFeelDecorated(true);newtest1();}}classTimerextendsThread//显示系统时间{privateJLabellt;privateSimpleDateFormatfy=newSimpleDateFormat(yyyy.MM.ddG'at'HH:mm:ssz);privateSimpleDateFormatfn=newSimpleDateFormat(yyyy.MM.ddG'at'HHmmssz);privatebooleanb=true;publicTimer(JLabellt){this.lt=lt;}publicvoidrun(){while(true){try{if(b){lt.setText(fy.format(newDate()));}else{lt.setText(fn.format(newDate()));}b=!b;Timer.sleep(500);}catch(InterruptedExceptionex){ex.printStackTrace();}}}}实验截图:publicclasstest1extendsJFrameimp允恃撮梧邯滋预哩睦共趴健赁攘郝熔坚钙烦答酞狮宛忆爬霉沂截催程度丽妹返眩涌鼓律矢贺萧续伊婉节谎摧私髓敲瘦友怜