Main.javaimport java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JFrame;public class Main extends JFrame{//创建面板Calendar Calendar=new Calendar();Clock clock=new Clock();public Main(){//设置布局为nullsetLayout(null);//设置关闭方式setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口位置和大小setBounds(300, 200, 600, 360);//将面板加入窗口add(Calendar);add(clock);//设置面板位置和大小Calendar.setBounds(0, 0, 400, 360);clock.setBounds(420, 100, 200, 200);//设置窗口关闭事件addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});//使窗口显示setVisible(true);}public static void main(String args[]){new Main();}}Clock.javaimport java.awt.*;import javax.swing.*;import java.awt.event.*;import java.util.Calendar;import java.awt.geom.*;public class Clock extends JPanel implements ActionListener { protected static Ellipse2D face = new Ellipse2D.Float(3,3,94,94); protected static GeneralPath tick = new GeneralPath(); //包含在java.awt.geom.*;static{tick.moveTo(100, 100);tick.moveTo(49, 0);tick.lineTo(51, 0);tick.lineTo(51, 6);tick.lineTo(49, 6);tick.lineTo(49, 0);}protected static GeneralPath hourHand = new GeneralPath();static{hourHand.moveTo(50, 15);hourHand.lineTo(53, 50);hourHand.lineTo(50, 53);hourHand.lineTo(47, 50);hourHand.lineTo(50, 15);}protected static GeneralPath minuteHand = new GeneralPath(); static{minuteHand.moveTo(50, 2); minuteHand.lineTo(53, 50); minuteHand.lineTo(50, 58);minuteHand.lineTo(47, 50);minuteHand.lineTo(50, 2);}protected static GeneralPath secondHand = new GeneralPath();static{secondHand.moveTo(49, 5);secondHand.lineTo(51, 5);secondHand.lineTo(51, 62);secondHand.lineTo(49, 62);secondHand.lineTo(49, 5);}protected static Color faceColor = new Color(220,220,220); protected static Color hourColor = Color.red.darker();protected static Color minuteColor = new Color(220,0,220); protected static Color secondColor = Color.blue.darker(); protected static Color pinColor = new Color(220,20,111);protected static Ellipse2D pivot = new Ellipse2D.Float(47,47,6,6); protected static Ellipse2D centerPin = newEllipse2D.Float(49,49,2,2);protected AffineTransform hourTransform =AffineTransform.getRotateInstance(0,50,50);protected AffineTransform minuteTransform =AffineTransform.getRotateInstance(0,50,50);protected AffineTransform secondTransform =AffineTransform.getRotateInstance(0,50,50);protected Timer timer = new Timer(1000,this);protected Calendar calender = Calendar.getInstance();public Clock(){setPreferredSize(new Dimension(100,100));}public void addNotify(){super.addNotify();timer.start();} public void removeNotify(){timer.stop();super.removeNotify();}public void actionPerformed (ActionEvent event){this.calender.setTime(new java.util.Date());int hour = this.calender.get(Calendar.HOUR);int minute = this.calender.get(Calendar.MINUTE);int second = this.calender.get(Calendar.SECOND);hourTransform.setToRotation((((double)(hour-1.0+minute/60.0))*(Ma th.PI/6.0)),50,50);minuteTransform.setToRotation(((double)(minute+second/60.0))*(Mat h.PI/30.0),50,50);secondTransform.setToRotation(((double)second)*(Math.PI/30.0),50, 50);repaint();}public void paint(Graphics g){super.paint(g);Graphics2D g2 = (Graphics2D) g;g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);GradientPaint pat1=newGradientPaint(0f,80f,Color.WHITE,100f,0f,new Color(200,25,250)); g2.setPaint(pat1);g2.fill(face);g2.setPaint(Color.black);g2.draw(face);g2.setPaint(hourColor);g2.fill(hourHand.createTransformedShape(hourTransform));g2.setPaint(minuteColor);g2.fill(minuteHand.createTransformedShape(minuteTransform));g2.setPaint(secondColor);g2.fill(secondHand.createTransformedShape(secondTransform));for (double p = 0.0; p < 12.0; p += 1.0) {g2.fill(tick.createTransformedShape(AffineTransform.getRotateInstance((Math.PI / 6.0) * p,50, 50)));}g2.fill(pivot);g2.setPaint(pinColor);g2.fill(centerPin);}}Calendar.javaimport java.awt.*;import java.awt.event.*;import ng.StringBuffer;import javax.swing.*;import java.util.*;import javax.swing.Timer;import javax.swing.border.*;public class Calendar extends JPanel implementsActionListener,ItemListener{Date date = new Date();private GregorianCalendar gregorianCalendar = newGregorianCalendar();//定义中英文字符数组存储星期信息,用于转换显示private String[] stringWeekEn= new String[] { "SUN", "MON", "TUE", "WED","THU", "FRI", "SAT" };private String[] stringWeekCn = new String[] { "星期日", "星期一", "星期二", "星期三","星期四", "星期五", "星期六" };//定义存储月份的信息数组,用于转换显示方示private String[] stringMonthEn= new String[] { "Jan", "Feb", "Mar", "Apr","May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" };private String[] stringMonthCn= {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};private String[] sysNowTime = new String[6];//sysNowTime 用于存储系统时间的变量private String[] sysRunTime = new String[6];private JLabel []labelWeek = new JLabel[7];private JLabel []labelDay = new JLabel[42];private JLabel labelTime = new JLabel();private JPanel panel1 = new JPanel();private JPanel panel2 = new JPanel();private JPanel panel3 = new JPanel();private JComboBox combo1 = new JComboBox();private JComboBox combo2 = new JComboBox();private JButton buttonToday = new JButton();private Border border = BorderFactory.createRaisedBevelBorder(); private Border border1 =BorderFactory.createLineBorder(Color.cyan,3);public Calendar() {for (int y = 1900; y < 2101; y++) {combo1.addItem(" " + new Integer(y).toString()+"年");}for (int m = 0;m<12;m++){combo2.addItem(" "+stringMonthCn[m]);}buttonToday.setText("今天");setLayout(new FlowLayout());add(panel1);add(panel2);add(panel3);panel1.setLayout(new GridLayout(1,3,10,0));panel1.add(combo1);combo1.addItemListener(this);panel1.add(combo2);combo2.addItemListener(this);panel1.add(buttonToday);buttonToday.addActionListener(this);labelTime.setFont(new Font("宋体",Font.PLAIN,16));labelTime.setForeground(Color.MAGENTA);panel1.add(labelTime);Timer time = new Timer(1000,new TimerListener());time.addActionListener(new TimerListener());//time.setRepeats(true);time.start();//labelTime.addAncestorListener(new TimerListener()); panel2.setLayout(new GridLayout(7,7,0,10));panel2.setBackground(Color.white);for(int i=0;i<7;i++){labelWeek[i] = new JLabel();labelWeek[i].setHorizontalAlignment(0);if(i==0||i==6){labelWeek[i].setBackground(Color.blue);labelWeek[i].setForeground(Color.RED);labelWeek[i].setFont(new Font("黑体",Font.BOLD,14)); }else{labelWeek[i].setForeground(Color.BLACK);labelWeek[i].setFont(new Font("新宋体",Font.PLAIN,14)); }labelWeek[i].setText(stringWeekCn[i]);panel2.add(labelWeek[i]);}for(int i= 0;i<42;i++){labelDay[i] = new JLabel();labelDay[i].setHorizontalAlignment(0);labelDay[i].setText("");panel2.add(labelDay[i]);}setSize(300,300);setBounds(250, 200, 400, 360);setVisible(true);getSysDate();setNowDate();}public void actionPerformed(ActionEvent ae){if(ae.getSource()==buttonToday){setNowDate();}}public void itemStateChanged(ItemEvent aa){setChangeDate();}public int turnWeek(String week){int i;for(i=0;i<7;i++)if(week.equalsIgnoreCase(stringWeekEn[i]))break;return i;}public int turnMonth(String month){/***int turnMonth(String month)*@month系统日期中的月,诸如Jan\Feb*@return int*返回一个整数值,用于寻找stringMonthCn[]数组中对应的中文月份*/int i;for(i=0;i<12;i++)if(month.equalsIgnoreCase(stringMonthEn[i]))break;return i;}/***setNowDate()*设置当前系统日期*/public void setNowDate(){setSysDate(getNowYear(),getNowMonth());getSysRunDate();setDateNull();combo1.setSelectedIndex(getShowYear() - 1900);combo2.setSelectedIndex(getShowMonth());setDays(getMonthDays(getNowYear(),getNowMonth()),getInitWeek(sysR unTime[0]),getNowDay());//labelTime.setText(sysNowTime[3]);//labelTime.setHorizontalAlignment(0);}/**Integer getShowYear()*获取组合框中应该显示的年份*/public void setSysDate(int year,int month){gregorianCalendar.set(year,month,1);}public void setDateNull(){for(int i=0;i<42;i++){labelDay[i].setText("");}}public void setChangeDate(){setSysDate(getComboYear(),getComboMonth());getSysRunDate();setDateNull();setDays(getMonthDays(getComboYear(),getComboMonth()),getInitWeek( sysRunTime[0]),-1);}public int getMonthDays(int year, int month) {/***返回所选年月的天数,因为数组中的数值从0开始,所以3\5\8\10分别代表4\6\9\11几个小月.*而1代表2月,经过是否为闰年判断,选择返回28或29天.*其余月份为大月,返回31天.**/switch (month) {case 3:case 5:case 8:case 10:return 30;//小月返回30天case 1:if (gregorianCalendar.isLeapYear(year)) {//isLeapYear(year)确定当前纪元中的指定年份是否为闰年。