《語言程序設計資料:java-萬年歷Java代碼.doc》由會員分享,可在線閱讀,更多相關《語言程序設計資料:java-萬年歷Java代碼.doc(8頁珍藏版)》請在匯文網上搜索。
1、import java.applet.*;import java.awt.*;import java.awt.event.*;import java.util.*; class Calendar extends Panel implements Runnable Button nextYear,nextMonth,nextDate,lastYear,lastMonth,lastDate; TextField year,month,date; AudioClip audioClip; Thread clock; Date today; String weekName=日,一,二,三,四,五,六;
2、 String monthName=一月,二月,三月,四月,五月,六月,七月,八月,九月,十月,十一月,十二月; int weekColor=0xff0000, 0xcc77ee, 0xcc77ee, 0xcc77ee, 0xcc77ee, 0xcc77ee, 0x7795c; Calendar(AudioClip au) today=new Date(); setLayout(new FlowLayout(FlowLayout.RIGHT,3,13); add(lastYear=new Button(-); add(year=new TextField (String.valueOf(190
3、0+today.getYear(),2); add(nextYear=new Button(+); add(lastMonth=new Button(-); add(month=new TextField(String.valueOf(today.getMonth()+1),1); add(nextMonth=new Button(+); add(lastDate=new Button(-); add(date=new TextField(String.valueOf(today.getDate(),1); add(nextDate=new Button(+); audioClip=au; p
4、ublic void run() long start=System.currentTimeMillis(); do repaint(); start+=1000; try Thread.sleep(Math.max(0,start-System.currentTimeMillis(); catch(InterruptedException e) while(true); public void paint(Graphics g) update(g); public void update(Graphics g) Dimension dimension=size(); Image image=
5、createImage(dimension.width,dimension.height); Graphics g1=image.getGraphics(); setBackground(Color.pink); g1.setColor(getBackground(); g1.fillRect(0,0,500,350); g1.setColor(new Color(0xfd6f60);/49 g1.drawOval(30,15,100,100); g1.fillOval(30,62,6,6); g1.fillOval(123,62,7,7); g1.fillOval(77,14,7,7); g
6、1.fillOval(77,108,7,7); g1.fillOval(37,39,3,3); g1.fillOval(55,22,3,3); g1.fillOval(102,22,3,3); g1.fillOval(120,39,3,3); g1.fillOval(37,86,3,3); g1.fillOval(55,106,3,3); g1.fillOval(102,106,5,3); g1.fillOval(120,86,3,3); Date nowTime=new Date(); double hours=(double)nowTime.getHours()+(double)nowTi
7、me.getMinutes()/60.0; double minutes=(double)nowTime.getMinutes() + (double)nowTime.getSeconds() /60.0; double second=nowTime.getSeconds(); g1.setColor(Color.black); g1.drawLine(80,64,(int)(81+30*Math.sin(hours*Math.PI/6),(int)(65-30 *Math.cos(hours*Math.PI/6); g1.drawLine(82,66,(int)(81+30*Math.sin
8、(hours*Math.PI/6),(int)(65-30 *Math.cos(hours*Math.PI/6); g1.drawLine(81,65,(int)(81+41*Math.sin(minutes*Math.PI/30),(int)(65-45 *Math.cos(minutes*Math.PI/30); g1.drawLine(80,64,(int)(81+41*Math.sin(minutes*Math.PI/30),(int)(65-45 *Math.cos(minutes*Math.PI/30); g1.setColor(Color.orange); g1.drawLine
9、(81,65,(int)(81+48*Math.sin(second*Math.PI/30),(int)(65-48 *Math.cos(second*Math.PI/30); g1.setColor(new Color(0xee77ee);/76 g1.setFont(new Font(TimesRoman,1,20); g1.drawString(monthNametoday.getMonth(),135,55); g1.setFont(new Font(TimesRoman,1,50); String s1=String.valueOf(today.getDate(); Color co
10、lor=new Color(weekColortoday.getDay(); /g1.setColor(Color.brighter); g1.drawString(s1,207,59); /g1.setColor(Color.darker); g1.drawString(s1,208,61); g1.setColor(color); g1.drawString(s1,206,60); g1.setColor(new Color(0xe3799f); String s=String.valueOf(1900+today.getYear(); g1.setFont(new Font(Courie
11、r,Font.ITALIC,60); for(int i=0;i4;i+) g1.drawString(s.substring(i,i+1),79,170+(i*45); s=開心每一天; g1.setFont(new Font(隸書,Font.PLAIN,20); for(int i=0;i5;i+) g1.drawString(s.substring(i,i+1),50,250+(i*20); g1.setColor(new Color(0x9d065d); g1.fillRect(145,95,340,230); g1.setColor(new Color(0xe3799f); g1.f
12、illRect(140,90,340,230); g1.setFont(new Font(Helvetica,1,20); for(int i=0;i7;i+) g1.setColor(Color.darkGray); g1.drawString(weekNamei,151+i*48,110); g1.setColor(Color.white); g1.drawString(weekNamei,149+i*48,108); g1.setFont(new Font(Serif,0,15); Date date=new Date(today.getYear(),today.getMonth(),1
13、); date.setDate(1-date.getDay(); for(int i=0;i6;i+) for(int j=0;j14&i113&j=1000&i=1&i=12) today.setMonth(i-1); repaint(); if(e.target.equals(date) i=Integer.valueOf(date.getText().intValue(); if(i!=today.getDate() Date date=new Date(today.getTime(); date.setDate(i); if(date.getMonth()=today.getMonth
14、() today.setDate(i); repaint(); catch(NumberFormatException e1) setDateText(); return true;/處理文本框輸入后按下回車事件public boolean keyDown(Event e,int i) if(i=10) return lostFocus(e,e.arg); else return super.keyDown(e,i); void setDateText() year.setText(String.valueOf(1900+today.getYear(); month.setText(Strin
15、g.valueOf(today.getMonth()+1); date.setText(String.valueOf(today.getDate(); /小程序public class eeqy extends Applet Calendar c; AudioClip audioClip1,audioClip2; public void init() String s=getParameter(musicname); audioClip1=getAudioClip(getCodeBase(),s); audioClip2=getAudioClip(getCodeBase(),ba.wav); audioClip1.loop(); c=new Calendar(audioClip2); setLayout(new BorderLayout(); add(Center,c); public void start() if(c.clock=null) c.clock=new Thread(c); c.clock.start(); public void stop() c.clock.stop(); c.clock=null; public void destroy() audioClip1.stop(); super.destroy(); HTML文件: