Posted on 2006-09-03 07:08
久城 閱讀(1007)
評論(0) 編輯 收藏 所屬分類:
JavaTest
學JAVA 做的第一個GUI程序......
import java.awt.*;
import javax.swing.*;
import java.util.*;
class Clock extends JFrame
{
?public static double PI = 3.14159265;
?Calendar now;
?int hh;
?int mm;
?int ss;
?Clock()
?{
??super("我的時鐘");
??setSize(400,400);
??setVisible(true);
?}
?public void paint(Graphics g)
?{
??g.setColor(Color.WHITE);
??g.fillRoundRect(100,100,200,200,200,200);
??g.setColor(Color.BLACK);
??g.drawString("1",(int)(200-2+100*(Math.sin(30*2*PI/360))),(int)(200+10-100*(Math.cos(30*2*PI/360))));
??g.drawString("2",(int)(200-6+100*(Math.sin(30*2*2*PI/360))),(int)(200+10-100*(Math.cos(30*2*2*PI/360))));
??g.drawString("3",(int)(200-7+100*(Math.sin(30*3*2*PI/360))),(int)(200-100*(Math.cos(30*3*2*PI/360))));
??g.drawString("4",(int)(200-6+100*(Math.sin(30*4*2*PI/360))),(int)(200-100*(Math.cos(30*4*2*PI/360))));
??g.drawString("5",(int)(200-2+100*(Math.sin(30*5*2*PI/360))),(int)(200-2-100*(Math.cos(30*5*2*PI/360))));
??g.drawString("6",(int)(200+100*(Math.sin(30*6*2*PI/360))),(int)(200-100*(Math.cos(30*6*2*PI/360))));
??g.drawString("7",(int)(200+100*(Math.sin(30*7*2*PI/360))),(int)(200-100*(Math.cos(30*7*2*PI/360))));
??g.drawString("8",(int)(200+100*(Math.sin(30*8*2*PI/360))),(int)(200-100*(Math.cos(30*8*2*PI/360))));
??g.drawString("9",(int)(200+100*(Math.sin(30*9*2*PI/360))),(int)(200-100*(Math.cos(30*9*2*PI/360))));
??g.drawString("10",(int)(200+2+100*(Math.sin(30*10*2*PI/360))),(int)(200+2-100*(Math.cos(30*10*2*PI/360))));
??g.drawString("11",(int)(200+100*(Math.sin(30*11*2*PI/360))),(int)(200+10-100*(Math.cos(30*11*2*PI/360))));
??g.drawString("12",(int)(200+100*(Math.sin(30*12*2*PI/360))),(int)(200+10-100*(Math.cos(30*12*2*PI/360))));
??g.setColor(Color.RED);
??Calendar now=Calendar.getInstance();
??int hh=now.get(Calendar.HOUR_OF_DAY);//小時
??int mm=now.get(Calendar.MINUTE);//分鐘
??int ss=now.get(Calendar.SECOND);// 秒
??g.drawLine(200,200,(int)(200+90*(Math.sin(6*ss*2*PI/360))),(int)(200-90*(Math.cos(6*ss*2*PI/360)))); //畫秒針
??g.setColor(Color.black);
??g.drawLine(200,200,(int)(200+70*(Math.sin(6*mm*2*PI/360))),(int)(200-70*(Math.cos(6*mm*2*PI/360))));//畫分針
??g.setColor(Color.blue);
??g.drawLine(200,200,(int)(200+50*(Math.sin((30*hh+0.5*mm)*2*PI/360))),(int)(200-50*(Math.cos((30*hh+0.5*mm)*2*PI/360)))); //畫時針
??try{Thread.sleep(500);}catch(Exception e){}
??repaint();??
?}
?public static void main(String[] args)
?{
??
??Clock c = new Clock();
??c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
??
?}
}
歡迎來訪!^.^!
本BLOG僅用于個人學習交流!
目的在于記錄個人成長.
所有文字均屬于個人理解.
如有錯誤,望多多指教!不勝感激!