📄 13.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">JavaApple中字体属性的设置</p>
<p> 从前地面的学习中,我们已经知道,Graphics类的方法drawString()可以在屏幕的指定位置显示一个字符串,而Java中还有一个类Font,使用它可以获得更加丰富多采和逼真精确的字体效果。</p>
<p> 一个Font类的对象表示了一种字体的显示效果,包括字体类型、字型、和字号。下面的语句用语创建一个Font类的对象:</p>
<p><font color="#FF9999">Font Myfont=new Font("楷体",Font.BOLD,14);</font></p>
<p> Myfont是自己刚刚设置好的一个字体属性,大小为14磅,类型为楷体,是加粗型,另外的两种是Font.PLAIN,
正常型,Font.ITALIC,斜体。</p>
<p> 选择好字体后,就是设置字体了,设置字体用一个方法为:g.setFont(Myfont)就可以了,以后在没有设置新的字体之前,都将会用这个已经设置好了的字体显示字体。给大家举下面一个例子,如下:</p>
<table width="75%" border="1" bordercolor="#FF6633">
<tr>
<td width="48%"> <applet
code=Ziti.class
name=Applet1
width=347
height=286 VIEWASTEXT>
<param name=vname value="张三">
<param name=vnumber value=4>
<param name=red value=255>
<param name=green value=0>
<param name=blue value=0>
</applet></td>
<td width="52%">
<p> 这个程序主要是讲述如何在Applet中调用新的字体,新的字体风格,字体大小,新的颜色以及如何从网页的HTML语法代码中获得相应的属性设置。</p>
<p> </p>
</td>
</tr>
</table>
<p>源代码如下:</p>
<pre>
import java.awt.*;
import java.applet.*;
public class Applet1 extends Applet
{
private String vname;
private int vnumber;
Color oldColor;
Font oldFont;
String[] paraname={"red","green","blue"}; //HTML提供的三个参数的名称
int[] rgb=new int[3]; //保存三色比例的数组
public void init()
{
vname=getParameter("vname"); //从网页中获得人名
vnumber=Integer.parseInt(getParameter("vnumber"));//从网页中获得号码
for(int i=0;i<paraname.length;i++) //取得HTML文件中指定的三色比例
rgb[i]=Integer.parseInt (getParameter(paraname[i]));//并转化成整数付给数组
}
public void paint(Graphics g)
{
String fontname[]={"宋体","黑体","录书"};//字体名字
String styname[]={"PLAIN","BOLD","ITALIC"};//字体风格名字
int fontstyle[]={Font.PLAIN ,Font.BOLD ,Font.ITALIC };//字体风格属性
oldFont=g.getFont (); //获得原来的缺省字体
for (int i=0;i<3;i++)
{
for (int j=0;j<3;j++)
{ //设置不同大小不同风格、类型的字体
g.setFont (new Font(fontname[i],fontstyle[j],12));
g.drawString ("请注意我的变化 "+fontname[i]+" "+
styname[j],20,20*(i*3+j+1));
}
} //循环打印出各个风格与类型的字符串
g.setFont (oldFont); //恢复原来的缺省字体
oldColor=g.getColor (); //保存原有的缺省颜色
g.drawString ("你好"+vname+", 你的号码是:"+vnumber,40,210);
g.setColor (new Color (rgb[0],rgb[1],rgb[2])); //设置新的颜色
g.drawString ("用的颜色是:"+g.getColor ().toString(),20,230);
//用新建颜色显示该颜色的三色分量
g.setColor (oldColor); //恢复原来的颜色
g.drawString ("以前的颜色是:"+g.getColor ().toString (),20,250);
//用原来的颜色显示原来颜色的三色分量
}
}
</pre>
<p>编译好这个文件后,在网页中要做相应的设置,如下:
<pre>
<font color="#FF9999"><applet code=Applet1.class width=320
height=270 VIEWASTEXT>
< param name=vname value="张三">
< <param name=vnumber value=4>
< param name=red value=255>
< param name=green value=0>
< param name=blue value=0>
</applet>
</font></pre>
<p> </p>
<p>上面的程序中涉及到了Color类, Color类的使用方法为:</p>
<p></p>
<p></p>
<p>Color Mycolor=new Color(int red,int Green,int blue);</p>
<p></p>
<p></p>
<p>例如:</p>
<p>Color Mycolor=new Color(0,0,255);</p>
<p>这就是一个创建蓝色的例子了。</p>
<p align="center"><a href="1.htm">[上一页]</a> <a href="../07/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 + -