?? 12.htm
字號:
<html><!-- #BeginTemplate "/Templates/empolder_doc.dwt" -->
<head>
<!-- #BeginEditable "doctitle" -->
<title>|><| 太平洋電腦信息網 -> 網絡學院 -> 開發教室</title>
<!-- #EndEditable -->
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
-->
</style>
<link rel="stylesheet" href="/pcedu/style/text.css"></head>
<body bgcolor="#FFFFFF" topmargin=0 leftmargin=0 marginheight="0">
<script language="JavaScript" src="/pcedu/script/top.js">
</script>
<table width=760 border=0 cellspacing=0 cellpadding=0 align=center>
<tr>
<td width=194 height="56"><a href=http://www.pconline.com.cn><img src=http://www.pconline.com.cn/images/pconlinelogo.gif width=162 height=35 vspace=10 border=0></a></td>
<td width=406 height="56">
<script language="JavaScript" src="/pcedu/script/empolder_ad.js">
</script>
</td>
<td width=158 align=right height="56">
<script language="JavaScript" src="/pcedu/script/empolder_ad1.js">
</script>
</td>
<td align=right width=2 height="56"> </td>
</tr>
</table>
<table border=0 cellpadding=0 cellspacing=0 width=760 align="center">
<tbody>
<tr valign=bottom>
<td rowspan=2 width=172><img height=32 src="/pcedu/images/pcedu_lo.gif"
width=172 border="0"></td>
<td height=30 rowspan=2>
<table cellpadding=0 cellspacing=0 width="588" bgcolor="#FFA000" background="/pcedu/images/e_menu5.gif" border="0">
<tbody>
<tr valign="bottom">
<td height="17">
<script language="JavaScript" src="/pcedu/script/title_empolder.js">
</script>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr></tr>
<tr bgcolor="#303880">
<td colspan=2 height=1 valign=bottom><img height=1
src="/pcedu/images/blank.gif" width=1></td>
</tr>
<tr>
<td colspan=2 height=5 valign=bottom><img height=5
src="/pcedu/images/blank.gif" width=1></td>
</tr>
</tbody>
</table>
<table width="760" cellspacing="0" cellpadding="0" align="center" height="37">
<tr>
<td width="170" valign="top">
<table border="0" width="170" height="100%"
cellspacing="1" bgcolor="#000000">
<tr bgcolor="#F8F8D2">
<td width="100%" valign="top"><!-- #BeginEditable "left" -->
<div align="center">
<table width="100%" border="0" cellspacing="1" cellpadding="0" bgcolor="#000000" align="center">
<tr bgcolor="#E17329">
<td height="20" align="center"><font color="#FFFFFF">==<b>開發教室==</b></font></td>
</tr>
</table>
<br>
</div>
<!-- #EndEditable -->
<script language="JavaScript" src="/pcedu/script/left_empolder.js">
</script>
</td>
</tr>
</table>
</td>
<td width="10"><img src="/pcedu/images/blank.gif" width="1" height="1"></td>
<td width="580" valign="top" class="article">
<p><img src="/pcedu/images/666666.gif" width="99%" height="1"></p>
<!-- #BeginEditable "content" -->
<p align="center"><span class="title">Java Applet 入門</span></p>
<p align="right"><a href="mailto:yy435@263.net">yy435</a></p>
<p align="right">太平洋網絡學院</p>
<p align="center" class="green">第五天</p>
<p align="center" class="green">一個時鐘的例子</p>
<table width="15%" border="1">
<tr bordercolor="#ff6666">
<td><applet
code=Applet1.class
name=Applet1
width=204
height=204 >
</applet></td>
</tr>
</table>
<p>源代碼如下:</p>
<pre>
import java.awt.*;
import java.applet.*;
import java.util.Date; <font color="#0033CC"> //這是Java中的低級實用工具包,可以處理時間等內容。</font>
public class Applet1 extends Applet implements Runnable <font color="#0033CC"> //有線程運行接口</font>
{
Date timenow; //Date是一個時間定義與創建函數.
Clock myClock; //用戶自定義的類
Thread clockthread=null; //設置一個線程
public void start() //線程開始的類
{
if (clockthread==null)<font color="#0033CC"> //如果線程為空,則</font>
{
clockthread=new Thread (this); //開始新的線程
clockthread.start(); //開始
}
}
public void stop() <font color="#0033CC"> //終止線程</font>
{
clockthread.stop(); //終止線程,使它
clockthread=null; //為空
}
public void run() //運行線程
{
while(true) //一個死循環,條件永遠都是真的。
{
repaint(); //重新繪制界面
try{Thread.sleep(1000);} //讓線程沉睡1000毫秒,也就是一秒鐘
catch(InterruptedException e){} //捕獲異常(也就是錯誤)
}
}
public void paint(Graphics g)
{
timenow=new Date(); //新的時間的獲得
//獲得小時,分鐘,秒鐘
myClock=new Clock(timenow.getHours (),
timenow.getMinutes (),
timenow.getSeconds ());
g.drawString(timenow.toString(),25,240);//將它打印出來!
myClock.show(g,100,100,100); //使面板顯示
}
}
class Clock //用戶自定義的類開始,編譯后,它單獨成為一個CLASS文件
{
Clock(int hrs,int min,int sec) <font color="#0033CC"> //類函數入口</font>
{
hour=hrs%12; //將原始數據處理,得到小時
minute=min; //將原始數據處理,得到分鐘
second=sec; //將原始數據處理,得到小時
}
void show(Graphics g,int cx,int cy,int rad) //重新定義SHOW函數
{
int hrs_len=(int)(rad*0.5), //時針的長度
min_len=(int)(rad*0.6),<font color="#0033CC"> //分鐘的長度</font>
sec_len=(int)(rad*0.9); //秒鐘的長度
double theta;
//畫出鐘面
g.drawOval(cx-rad,cy-rad,rad*2,rad*2);
//畫出時針
theta=(double)(hour*60*60+minute*60+second)/43200.0*2.0*Math.PI ;
drawNiddle(g,Color.blue,cx,cy,hrs_len,theta);
//畫出分針
theta=(double)(minute*60+second)/3600.0*2.0*Math.PI ;
drawNiddle(g,Color.red,cx,cy,sec_len,theta);
//畫出秒針
theta=(double)(second)/60.0*2.0*Math.PI ;
drawNiddle(g,Color.green ,cx,cy,sec_len,theta);
}
private void drawNiddle(Graphics g,Color c,int x,int y,int len,double theta)
{
int ex=(int)(x+len*Math.sin(theta));
int ey=(int)(y-len*Math.cos(theta));
g.setColor (c);
g.drawLine(x,y,ex,ey);
}
int hour,minute,second;
}
</pre>
<p align="center"><a href="1.htm">[上一頁]</a> <a href="../06/1.htm">[下一頁]</a></p>
<!-- #EndEditable -->
<div align="center">
<table width="100%" border="0">
<tr class="sfont">
<td>
<div align="center"><a href="javascript:history.go(-1)">[返回]</a>
</div>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
<hr noshade size="2" width="760">
<div align="center">
<p align="center"><font color="#000000">
<script language="JavaScript" src="/pcedu/script/title_edu.js">
</script>
</font> <br>
<br>
版權所有©2000 太平洋電腦網<br>
<font face="Arial, Helvetica, sans-serif">
<script>
document.write("<a href=http://best.netease.com/cgi-bin/view/viewbasic.cgi?exp target=_blank><img src=http://best.netease.com/cgi-bin/log.cgi?user=exp&refer="+escape(document.referrer)+"&cur="+escape(document.URL)+" border=0 alt='網易中文排行榜' width=1 height=1></a>");
</script>
<a href=mailto:webmaster@pconline.com.cn> </a></font><font face="Arial, Helvetica, sans-serif"><a href=mailto:webmaster@pconline.com.cn>webmaster@pconline.com.cn</a></font><font face="Arial, Helvetica, sans-serif"><a href=mailto:webmaster@pconline.com.cn>
<script language="">document.write("<a href=http://best.netease.com/cgi-bin/view/viewbasic.cgi?pconline1 target=_blank><img src=http://best.netease.com/cgi-bin/log.cgi?user=pconline1&refer="+escape(document.referrer)+"&cur="+escape(document.URL)+" border=0 width=1 height=1 ></a>");</script>
</a>
<script language="">
document.write("<a href=http://www0.pconline.com.cn:8810/pccount/index.php target=_blank><img src=http://www0.pconline.com.cn:8810/cgi-bin/test.cgi?user=pcedu&refer="+escape(document.referrer)+"&cur="+escape(document.URL)+" border=0 width=0 height=0 alt='' ></a>");
</script>
</font> </p>
</div>
</body>
<!-- #EndTemplate --></html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -