﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-欢迎来到小米的博客-随笔分类-Java</title><link>http://www.blogjava.net/georgehill/category/1273.html</link><description>希望能和您交流Java编程的知识和见解</description><language>zh-cn</language><lastBuildDate>Tue, 27 Feb 2007 15:53:13 GMT</lastBuildDate><pubDate>Tue, 27 Feb 2007 15:53:13 GMT</pubDate><ttl>60</ttl><item><title>由JComponent生成BufferedImage</title><link>http://www.blogjava.net/georgehill/archive/2006/04/14/41179.html</link><dc:creator>小米</dc:creator><author>小米</author><pubDate>Fri, 14 Apr 2006 15:41:00 GMT</pubDate><guid>http://www.blogjava.net/georgehill/archive/2006/04/14/41179.html</guid><wfw:comment>http://www.blogjava.net/georgehill/comments/41179.html</wfw:comment><comments>http://www.blogjava.net/georgehill/archive/2006/04/14/41179.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/georgehill/comments/commentRss/41179.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/georgehill/services/trackbacks/41179.html</trackback:ping><description><![CDATA[最近在弄swing，需要由JComponent生成BufferedImage，在CSDN上发现一个好例子。下面是范例：<br /><br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #000000">Rectangle rect </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> comp.getBounds();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /> BufferedImage bufImage </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> BufferedImage(rect.width,<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />           rect.height,<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />           BufferedImage.TYPE_INT_RGB);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /> Graphics g </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> bufImage.getGraphics();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /> g.translate(</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">rect.x, </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">rect.y);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /> comp.paint(g);</span></div><br />这样，JComponent中的图像就保存到BufferedImage中了。<br />原文的链接：<a href="http://dev.csdn.net/article/13/13531.shtm">http://dev.csdn.net/article/13/13531.shtm</a><img src ="http://www.blogjava.net/georgehill/aggbug/41179.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/georgehill/" target="_blank">小米</a> 2006-04-14 23:41 <a href="http://www.blogjava.net/georgehill/archive/2006/04/14/41179.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>如何将BufferedImage实例保存为BMP文件</title><link>http://www.blogjava.net/georgehill/archive/2005/07/04/7101.html</link><dc:creator>小米</dc:creator><author>小米</author><pubDate>Mon, 04 Jul 2005 09:33:00 GMT</pubDate><guid>http://www.blogjava.net/georgehill/archive/2005/07/04/7101.html</guid><wfw:comment>http://www.blogjava.net/georgehill/comments/7101.html</wfw:comment><comments>http://www.blogjava.net/georgehill/archive/2005/07/04/7101.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.blogjava.net/georgehill/comments/commentRss/7101.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/georgehill/services/trackbacks/7101.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;要将BufferedImage实例保存为BMP文件，就需要知道BMP文件的格式，可以参考我转载的文章：《BMP文件格式》。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;下面是我的将BufferedImage实例保存为24位色BMP文件的实现。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;<a href='http://www.blogjava.net/georgehill/archive/2005/07/04/7101.html'>阅读全文</a><img src ="http://www.blogjava.net/georgehill/aggbug/7101.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/georgehill/" target="_blank">小米</a> 2005-07-04 17:33 <a href="http://www.blogjava.net/georgehill/archive/2005/07/04/7101.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关闭JBuilder2005的Smart MemberInsight功能</title><link>http://www.blogjava.net/georgehill/archive/2005/06/15/6164.html</link><dc:creator>小米</dc:creator><author>小米</author><pubDate>Wed, 15 Jun 2005 08:02:00 GMT</pubDate><guid>http://www.blogjava.net/georgehill/archive/2005/06/15/6164.html</guid><wfw:comment>http://www.blogjava.net/georgehill/comments/6164.html</wfw:comment><comments>http://www.blogjava.net/georgehill/archive/2005/06/15/6164.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/georgehill/comments/commentRss/6164.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/georgehill/services/trackbacks/6164.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JBuilder 2005中多了一个Smart MemberInsight功能，这个功能会智能的匹配需要调用的方法或者获取的属性。例如有一个赋值操作，左边是一个List，右边是用一个方法获取值，那么当输入"."时，JBuilder 2005会自动的找到所有返回的值为List的方法。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;不过，这实在是一个不怎么讨好的方法。因为很多时候并不是只有一层方法调用的，例如，当需要用一个方法获取一个实例，然后再用这个实例的方法获取需要的值时，这个功能就显得很碍手了。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;终于，我还是决定关闭这个“智能”的功能。打开"Tools"——&gt;"Preferences"，在"Editor"的子项"CodeInsight"里，展开"MemberInsight"，把"Default to Smart MemberInsight"这一项去掉。如下图所示：<BR><BR><IMG height=476 alt=memberinsight.JPG src="http://www.blogjava.net/images/blogjava_net/georgehill/temp/memberinsight.JPG" width=587 border=0><img src ="http://www.blogjava.net/georgehill/aggbug/6164.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/georgehill/" target="_blank">小米</a> 2005-06-15 16:02 <a href="http://www.blogjava.net/georgehill/archive/2005/06/15/6164.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用JFreeChart画柱状图的范例</title><link>http://www.blogjava.net/georgehill/archive/2005/06/14/6123.html</link><dc:creator>小米</dc:creator><author>小米</author><pubDate>Tue, 14 Jun 2005 10:40:00 GMT</pubDate><guid>http://www.blogjava.net/georgehill/archive/2005/06/14/6123.html</guid><wfw:comment>http://www.blogjava.net/georgehill/comments/6123.html</wfw:comment><comments>http://www.blogjava.net/georgehill/archive/2005/06/14/6123.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.blogjava.net/georgehill/comments/commentRss/6123.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/georgehill/services/trackbacks/6123.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;这个范例说明如何用JFreeChart画简单的柱状图，下面是一个JSP的简单范例：<BR><BR>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">&lt;%</SPAN><SPAN style="COLOR: #000000">@&nbsp;page&nbsp;contentType</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">text/html;&nbsp;charset=GB2312</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">%&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;%</SPAN><SPAN style="COLOR: #000000">@&nbsp;page&nbsp;import</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">java.awt.*,&nbsp;java.text.*,&nbsp;java.util.*</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">%&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;%</SPAN><SPAN style="COLOR: #000000">@&nbsp;page&nbsp;import</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">org.jfree.chart.*</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">%&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;%</SPAN><SPAN style="COLOR: #000000">@&nbsp;page&nbsp;import</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">org.jfree.chart.axis.*</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">%&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;%</SPAN><SPAN style="COLOR: #000000">@&nbsp;page&nbsp;import</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">org.jfree.chart.labels.StandardCategoryItemLabelGenerator</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">%&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;%</SPAN><SPAN style="COLOR: #000000">@&nbsp;page&nbsp;import</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">org.jfree.chart.plot.*</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">%&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;%</SPAN><SPAN style="COLOR: #000000">@&nbsp;page&nbsp;import</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">org.jfree.chart.renderer.*</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">%&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;%</SPAN><SPAN style="COLOR: #000000">@&nbsp;page&nbsp;import</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">org.jfree.chart.servlet.ServletUtilities</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">%&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;%</SPAN><SPAN style="COLOR: #000000">@&nbsp;page&nbsp;import</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">org.jfree.data.DefaultCategoryDataset</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">%&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;%</SPAN><SPAN style="COLOR: #000000">@&nbsp;page&nbsp;import</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">org.jfree.ui.TextAnchor</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">%&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;%</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">The&nbsp;data&nbsp;for&nbsp;the&nbsp;bar&nbsp;chart</SPAN><SPAN style="COLOR: #008000"><BR><IMG id=Codehighlighter1_583_608_Open_Image onclick="this.style.display='none'; Codehighlighter1_583_608_Open_Text.style.display='none'; Codehighlighter1_583_608_Closed_Image.style.display='inline'; Codehighlighter1_583_608_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_583_608_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_583_608_Closed_Text.style.display='none'; Codehighlighter1_583_608_Open_Image.style.display='inline'; Codehighlighter1_583_608_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">double</SPAN><SPAN style="COLOR: #000000">[]&nbsp;data&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN id=Codehighlighter1_583_608_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_583_608_Open_Text><SPAN style="COLOR: #000000">{</SPAN><SPAN style="COLOR: #000000">85</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">156</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">179.5</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">211</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">123</SPAN><SPAN style="COLOR: #000000">}</SPAN></SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">The&nbsp;labels&nbsp;for&nbsp;the&nbsp;bar&nbsp;chart</SPAN><SPAN style="COLOR: #008000"><BR><IMG id=Codehighlighter1_664_698_Open_Image onclick="this.style.display='none'; Codehighlighter1_664_698_Open_Text.style.display='none'; Codehighlighter1_664_698_Closed_Image.style.display='inline'; Codehighlighter1_664_698_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_664_698_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_664_698_Closed_Text.style.display='none'; Codehighlighter1_664_698_Open_Image.style.display='inline'; Codehighlighter1_664_698_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;String[]&nbsp;labels&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN id=Codehighlighter1_664_698_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_664_698_Open_Text><SPAN style="COLOR: #000000">{</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Mon</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Tue</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Wed</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Thu</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Fri</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">}</SPAN></SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;DefaultCategoryDataset&nbsp;dataset&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;DefaultCategoryDataset();<BR><IMG id=Codehighlighter1_809_861_Open_Image onclick="this.style.display='none'; Codehighlighter1_809_861_Open_Text.style.display='none'; Codehighlighter1_809_861_Closed_Image.style.display='inline'; Codehighlighter1_809_861_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_809_861_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_809_861_Closed_Text.style.display='none'; Codehighlighter1_809_861_Open_Image.style.display='inline'; Codehighlighter1_809_861_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;data.length;&nbsp;i</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN id=Codehighlighter1_809_861_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_809_861_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;dataset.addValue(data[i],&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">,&nbsp;labels[i]);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;JFreeChart&nbsp;chart&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;ChartFactory.createBarChart3D(</SPAN><SPAN style="COLOR: #000000">"Weekly Server Load"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">"Work Week 25"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">"MBytes"</SPAN><SPAN style="COLOR: #000000">,&nbsp;dataset,&nbsp;PlotOrientation.VERTICAL,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">false</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">false</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">false</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;chart.setBackgroundPaint(</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Color(</SPAN><SPAN style="COLOR: #000000">0xE1E1E1</SPAN><SPAN style="COLOR: #000000">));<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;CategoryPlot&nbsp;plot&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;chart.getCategoryPlot();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;设置Y轴显示整数</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;NumberAxis&nbsp;rangeAxis&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(NumberAxis)&nbsp;plot.getRangeAxis();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;CategoryAxis&nbsp;domainAxis&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;plot.getDomainAxis();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">设置距离图片左端距离</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;domainAxis.setLowerMargin(</SPAN><SPAN style="COLOR: #000000">0.05</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;BarRenderer3D&nbsp;renderer&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;BarRenderer3D();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">设置柱的颜色</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;renderer.setSeriesPaint(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Color(</SPAN><SPAN style="COLOR: #000000">0xff00</SPAN><SPAN style="COLOR: #000000">));<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;plot.setRenderer(renderer);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;String&nbsp;filename&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;ServletUtilities.saveChartAsPNG(chart,&nbsp;</SPAN><SPAN style="COLOR: #000000">300</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">280</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">,&nbsp;session);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;String&nbsp;graphURL&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;request.getContextPath()&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">/displayChart?filename=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;filename;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">%&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">html</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">body&nbsp;topmargin</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">5</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;leftmargin</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">5</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;rightmargin</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">div&nbsp;style</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">font-size:18pt;&nbsp;font-family:verdana;&nbsp;font-weight:bold</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;3D&nbsp;Bar&nbsp;Chart<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">div</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">br</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">img&nbsp;src</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&lt;%=&nbsp;graphURL&nbsp;%&gt;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;border</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">body</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">html</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></DIV></DIV>
<P><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;画出来的图：<BR><BR><IMG height=280 alt=displayChart.JPG src="http://www.blogjava.net/images/blogjava_net/georgehill/temp/displayChart.JPG" width=300 border=0><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;和ChartDirector画出来的图做一个比较：<BR><BR><IMG height=280 alt=threedbar.JPG src="http://www.blogjava.net/images/blogjava_net/georgehill/temp/threedbar.JPG" width=300 border=0><BR></P><img src ="http://www.blogjava.net/georgehill/aggbug/6123.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/georgehill/" target="_blank">小米</a> 2005-06-14 18:40 <a href="http://www.blogjava.net/georgehill/archive/2005/06/14/6123.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用ChartDirector在JSP中画统计图</title><link>http://www.blogjava.net/georgehill/archive/2005/06/14/6118.html</link><dc:creator>小米</dc:creator><author>小米</author><pubDate>Tue, 14 Jun 2005 09:46:00 GMT</pubDate><guid>http://www.blogjava.net/georgehill/archive/2005/06/14/6118.html</guid><wfw:comment>http://www.blogjava.net/georgehill/comments/6118.html</wfw:comment><comments>http://www.blogjava.net/georgehill/archive/2005/06/14/6118.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.blogjava.net/georgehill/comments/commentRss/6118.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/georgehill/services/trackbacks/6118.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;以前一直是用JFreeChart画统计图的，不过JFreeChart画出来的图形不够精细，看起来有些模糊，今天在网上看到另外一个工具ChartDirector，这是一个商业版本的工具，不过也可以免费使用，只是在画出来的图形下面都有一条它的广告条。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;下面是它的一个柱状图的例子：<BR><BR><IMG height=280 alt=threedbar.JPG src="http://www.blogjava.net/images/blogjava_net/georgehill/temp/threedbar.JPG" width=300 border=0><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;范例程序：<BR><BR>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">&lt;%</SPAN><SPAN style="COLOR: #000000">@page&nbsp;import</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">ChartDirector.*</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">%&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;%</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">The&nbsp;data&nbsp;for&nbsp;the&nbsp;bar&nbsp;chart</SPAN><SPAN style="COLOR: #008000"><BR><IMG id=Codehighlighter1_84_109_Open_Image onclick="this.style.display='none'; Codehighlighter1_84_109_Open_Text.style.display='none'; Codehighlighter1_84_109_Closed_Image.style.display='inline'; Codehighlighter1_84_109_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_84_109_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_84_109_Closed_Text.style.display='none'; Codehighlighter1_84_109_Open_Image.style.display='inline'; Codehighlighter1_84_109_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">double</SPAN><SPAN style="COLOR: #000000">[]&nbsp;data&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN id=Codehighlighter1_84_109_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_84_109_Open_Text><SPAN style="COLOR: #000000">{</SPAN><SPAN style="COLOR: #000000">85</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">156</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">179.5</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">211</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">123</SPAN><SPAN style="COLOR: #000000">}</SPAN></SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">The&nbsp;labels&nbsp;for&nbsp;the&nbsp;bar&nbsp;chart</SPAN><SPAN style="COLOR: #008000"><BR><IMG id=Codehighlighter1_162_196_Open_Image onclick="this.style.display='none'; Codehighlighter1_162_196_Open_Text.style.display='none'; Codehighlighter1_162_196_Closed_Image.style.display='inline'; Codehighlighter1_162_196_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_162_196_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_162_196_Closed_Text.style.display='none'; Codehighlighter1_162_196_Open_Image.style.display='inline'; Codehighlighter1_162_196_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">String[]&nbsp;labels&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN id=Codehighlighter1_162_196_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_162_196_Open_Text><SPAN style="COLOR: #000000">{</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Mon</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Tue</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Wed</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Thu</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Fri</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">}</SPAN></SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">Create&nbsp;a&nbsp;XYChart&nbsp;object&nbsp;of&nbsp;size&nbsp;300&nbsp;x&nbsp;280&nbsp;pixels</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">XYChart&nbsp;c&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;XYChart(</SPAN><SPAN style="COLOR: #000000">300</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">280</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">Set&nbsp;the&nbsp;plotarea&nbsp;at&nbsp;(45,&nbsp;30)&nbsp;and&nbsp;of&nbsp;size&nbsp;200&nbsp;x&nbsp;200&nbsp;pixels</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">c.setPlotArea(</SPAN><SPAN style="COLOR: #000000">45</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">30</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">200</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">200</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">Add&nbsp;a&nbsp;title&nbsp;to&nbsp;the&nbsp;chart</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">c.addTitle(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Weekly&nbsp;Server&nbsp;Load</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">Add&nbsp;a&nbsp;title&nbsp;to&nbsp;the&nbsp;y&nbsp;axis</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">c.yAxis().setTitle(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">MBytes</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">Add&nbsp;a&nbsp;title&nbsp;to&nbsp;the&nbsp;x&nbsp;axis</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">c.xAxis().setTitle(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Work&nbsp;Week&nbsp;25</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">Add&nbsp;a&nbsp;bar&nbsp;chart&nbsp;layer&nbsp;with&nbsp;green&nbsp;(0x00ff00)&nbsp;bars&nbsp;using&nbsp;the&nbsp;given&nbsp;data</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">c.addBarLayer(data,&nbsp;</SPAN><SPAN style="COLOR: #000000">0xff00</SPAN><SPAN style="COLOR: #000000">).set3D();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">Set&nbsp;the&nbsp;labels&nbsp;on&nbsp;the&nbsp;x&nbsp;axis.</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">c.xAxis().setLabels(labels);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">output&nbsp;the&nbsp;chart</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">String&nbsp;chart1URL&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;c.makeSession(request,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">chart1</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">include&nbsp;tool&nbsp;tip&nbsp;for&nbsp;the&nbsp;chart</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">String&nbsp;imageMap1&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;c.getHTMLImageMap(</SPAN><SPAN style="COLOR: #000000">""</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">""</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">title='{xLabel}:&nbsp;{value}&nbsp;MBytes'</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">)<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">%&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">html</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">body&nbsp;topmargin</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">5</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;leftmargin</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">5</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;rightmargin</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">div&nbsp;style</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">font-size:18pt;&nbsp;font-family:verdana;&nbsp;font-weight:bold</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;3D&nbsp;Bar&nbsp;Chart<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">div</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">hr&nbsp;color</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">#000080</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">a&nbsp;href</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">viewsource.jsp?file=&lt;%=request.getServletPath()%&gt;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">font&nbsp;size</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;face</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Verdana</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">View&nbsp;Chart&nbsp;Source&nbsp;Code</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">font</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">a</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">div</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">br</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">img&nbsp;src</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">&lt;%=response.encodeURL("getchart.jsp?"+chart1URL)%&gt;</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;usemap</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">#map1</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;border</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">map&nbsp;name</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">map1</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;&lt;%=</SPAN><SPAN style="COLOR: #000000">imageMap1</SPAN><SPAN style="COLOR: #000000">%&gt;&lt;/</SPAN><SPAN style="COLOR: #000000">map</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">body</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">html</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></DIV></DIV>
<P><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;如果要在柱的顶部显示数值，可以调用Layer的setDataLabelFormat方法设置，范例：layer.setDataLabelFormat("{value}");<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;其它的例子可以参考它的文档的说明。ChartDirector的网址：<A href="http://www.advsofteng.com">http://www.advsofteng.com</A></P><img src ="http://www.blogjava.net/georgehill/aggbug/6118.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/georgehill/" target="_blank">小米</a> 2005-06-14 17:46 <a href="http://www.blogjava.net/georgehill/archive/2005/06/14/6118.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用java.util.Timer定时执行任务</title><link>http://www.blogjava.net/georgehill/archive/2005/06/09/5793.html</link><dc:creator>小米</dc:creator><author>小米</author><pubDate>Thu, 09 Jun 2005 02:29:00 GMT</pubDate><guid>http://www.blogjava.net/georgehill/archive/2005/06/09/5793.html</guid><wfw:comment>http://www.blogjava.net/georgehill/comments/5793.html</wfw:comment><comments>http://www.blogjava.net/georgehill/archive/2005/06/09/5793.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.blogjava.net/georgehill/comments/commentRss/5793.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/georgehill/services/trackbacks/5793.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;如果要在程序中定时执行任务，可以使用java.util.Timer这个类实现。使用Timer类需要一个继承了java.util.TimerTask的类。TimerTask是一个虚类，需要实现它的run方法，实际上是他implements了Runnable接口，而把run方法留给子类实现。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;下面是我的一个例子：<BR><BR>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG id=Codehighlighter1_31_93_Open_Image onclick="this.style.display='none'; Codehighlighter1_31_93_Open_Text.style.display='none'; Codehighlighter1_31_93_Closed_Image.style.display='inline'; Codehighlighter1_31_93_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_31_93_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_31_93_Closed_Text.style.display='none'; Codehighlighter1_31_93_Open_Image.style.display='inline'; Codehighlighter1_31_93_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;Worker&nbsp;extends&nbsp;TimerTask&nbsp;</SPAN><SPAN id=Codehighlighter1_31_93_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_31_93_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG id=Codehighlighter1_53_91_Open_Image onclick="this.style.display='none'; Codehighlighter1_53_91_Open_Text.style.display='none'; Codehighlighter1_53_91_Closed_Image.style.display='inline'; Codehighlighter1_53_91_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_53_91_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_53_91_Closed_Text.style.display='none'; Codehighlighter1_53_91_Open_Image.style.display='inline'; Codehighlighter1_53_91_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;run()&nbsp;</SPAN><SPAN id=Codehighlighter1_53_91_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_53_91_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;System.</SPAN><SPAN style="COLOR: #0000ff">out</SPAN><SPAN style="COLOR: #000000">.println(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">我在工作啦！</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV></DIV><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Timer类用schedule方法或者scheduleAtFixedRate方法启动定时执行，schedule重载了四个版本，scheduleAtFixedRate重载了两个。每个方法的实现都不同，下面是每个方法的说明：<BR><BR>
<H3>schedule</H3><PRE>public void <B>schedule</B>(<A title="class in java.util" href="file:///C:/Program%20Files/Java/jdk1.5.0_02/docs/api/java/util/TimerTask.html">TimerTask</A>&nbsp;task,
                     long&nbsp;delay)</PRE>
<DL>
<DD>Schedules the specified task for execution after the specified delay. 
<P></P>
<DD>
<DL>
<DT><B>Parameters:</B> 
<DD><CODE>task</CODE> - task to be scheduled. 
<DD><CODE>delay</CODE> - delay in milliseconds before task is to be executed. 
<DT><B>Throws:</B> 
<DD><CODE><A title="class in java.lang" href="file:///C:/Program%20Files/Java/jdk1.5.0_02/docs/api/java/lang/IllegalArgumentException.html">IllegalArgumentException</A></CODE> - if <TT>delay</TT> is negative, or <TT>delay + System.currentTimeMillis()</TT> is negative. 
<DD><CODE><A title="class in java.lang" href="file:///C:/Program%20Files/Java/jdk1.5.0_02/docs/api/java/lang/IllegalStateException.html">IllegalStateException</A></CODE> - if task was already scheduled or cancelled, or timer was cancelled.<BR></DD></DL></DD></DL>说明：该方法会在设定的延时后执行一次任务。<BR>
<HR>
<A name="schedule(java.util.TimerTask, java.util.Date)"><!-- --></A>
<H3>schedule</H3><PRE>public void <B>schedule</B>(<A title="class in java.util" href="file:///C:/Program%20Files/Java/jdk1.5.0_02/docs/api/java/util/TimerTask.html">TimerTask</A>&nbsp;task,
                     <A title="class in java.util" href="file:///C:/Program%20Files/Java/jdk1.5.0_02/docs/api/java/util/Date.html">Date</A>&nbsp;time)</PRE>
<DL>
<DD>Schedules the specified task for execution at the specified time. If the time is in the past, the task is scheduled for immediate execution. 
<P></P>
<DD>
<DL>
<DT><B>Parameters:</B> 
<DD><CODE>task</CODE> - task to be scheduled. 
<DD><CODE>time</CODE> - time at which task is to be executed. 
<DT><B>Throws:</B> 
<DD><CODE><A title="class in java.lang" href="file:///C:/Program%20Files/Java/jdk1.5.0_02/docs/api/java/lang/IllegalArgumentException.html">IllegalArgumentException</A></CODE> - if <TT>time.getTime()</TT> is negative. 
<DD><CODE><A title="class in java.lang" href="file:///C:/Program%20Files/Java/jdk1.5.0_02/docs/api/java/lang/IllegalStateException.html">IllegalStateException</A></CODE> - if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated.</DD></DL></DD></DL>说明：该方法会在指定的时间点执行一次任务。<BR>
<HR>
<A name="schedule(java.util.TimerTask, long, long)"><!-- --></A>
<H3>schedule</H3><PRE>public void <B>schedule</B>(<A title="class in java.util" href="file:///C:/Program%20Files/Java/jdk1.5.0_02/docs/api/java/util/TimerTask.html">TimerTask</A>&nbsp;task,
                     long&nbsp;delay,
                     long&nbsp;period)</PRE>
<DL>
<DD>Schedules the specified task for repeated <I>fixed-delay execution</I>, beginning after the specified delay. Subsequent executions take place at approximately regular intervals separated by the specified period. 
<P>In fixed-delay execution, each execution is scheduled relative to the actual execution time of the previous execution. If an execution is delayed for any reason (such as garbage collection or other background activity), subsequent executions will be delayed as well. In the long run, the frequency of execution will generally be slightly lower than the reciprocal of the specified period (assuming the system clock underlying <TT>Object.wait(long)</TT> is accurate). 
<P>Fixed-delay execution is appropriate for recurring activities that require "smoothness." In other words, it is appropriate for activities where it is more important to keep the frequency accurate in the short run than in the long run. This includes most animation tasks, such as blinking a cursor at regular intervals. It also includes tasks wherein regular activity is performed in response to human input, such as automatically repeating a character as long as a key is held down. 
<P></P>
<DD>
<DL>
<DT><B>Parameters:</B> 
<DD><CODE>task</CODE> - task to be scheduled. 
<DD><CODE>delay</CODE> - delay in milliseconds before task is to be executed. 
<DD><CODE>period</CODE> - time in milliseconds between successive task executions. 
<DT><B>Throws:</B> 
<DD><CODE><A title="class in java.lang" href="file:///C:/Program%20Files/Java/jdk1.5.0_02/docs/api/java/lang/IllegalArgumentException.html">IllegalArgumentException</A></CODE> - if <TT>delay</TT> is negative, or <TT>delay + System.currentTimeMillis()</TT> is negative. 
<DD><CODE><A title="class in java.lang" href="file:///C:/Program%20Files/Java/jdk1.5.0_02/docs/api/java/lang/IllegalStateException.html">IllegalStateException</A></CODE> - if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated.</DD></DL></DD></DL>说明：该方法会在指定的延时后执行任务，并且在设定的周期定时执行任务。<BR>
<HR>
<A name="schedule(java.util.TimerTask, java.util.Date, long)"><!-- --></A>
<H3>schedule</H3><PRE>public void <B>schedule</B>(<A title="class in java.util" href="file:///C:/Program%20Files/Java/jdk1.5.0_02/docs/api/java/util/TimerTask.html">TimerTask</A>&nbsp;task,
                     <A title="class in java.util" href="file:///C:/Program%20Files/Java/jdk1.5.0_02/docs/api/java/util/Date.html">Date</A>&nbsp;firstTime,
                     long&nbsp;period)</PRE>
<DL>
<DD>Schedules the specified task for repeated <I>fixed-delay execution</I>, beginning at the specified time. Subsequent executions take place at approximately regular intervals, separated by the specified period. 
<P>In fixed-delay execution, each execution is scheduled relative to the actual execution time of the previous execution. If an execution is delayed for any reason (such as garbage collection or other background activity), subsequent executions will be delayed as well. In the long run, the frequency of execution will generally be slightly lower than the reciprocal of the specified period (assuming the system clock underlying <TT>Object.wait(long)</TT> is accurate). 
<P>Fixed-delay execution is appropriate for recurring activities that require "smoothness." In other words, it is appropriate for activities where it is more important to keep the frequency accurate in the short run than in the long run. This includes most animation tasks, such as blinking a cursor at regular intervals. It also includes tasks wherein regular activity is performed in response to human input, such as automatically repeating a character as long as a key is held down. 
<P></P>
<DD>
<DL>
<DT><B>Parameters:</B> 
<DD><CODE>task</CODE> - task to be scheduled. 
<DD><CODE>firstTime</CODE> - First time at which task is to be executed. 
<DD><CODE>period</CODE> - time in milliseconds between successive task executions. 
<DT><B>Throws:</B> 
<DD><CODE><A title="class in java.lang" href="file:///C:/Program%20Files/Java/jdk1.5.0_02/docs/api/java/lang/IllegalArgumentException.html">IllegalArgumentException</A></CODE> - if <TT>time.getTime()</TT> is negative. 
<DD><CODE><A title="class in java.lang" href="file:///C:/Program%20Files/Java/jdk1.5.0_02/docs/api/java/lang/IllegalStateException.html">IllegalStateException</A></CODE> - if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated.</DD></DL></DD></DL>说明：该方法会在指定的时间点执行任务，然后从该时间点开始，在设定的周期定时执行任务。特别的，如果设定的时间点在当前时间之前，任务会被马上执行，然后开始按照设定的周期定时执行任务。<BR>
<HR>
<A name="scheduleAtFixedRate(java.util.TimerTask, long, long)"><!-- --></A>
<H3>scheduleAtFixedRate</H3><PRE>public void <B>scheduleAtFixedRate</B>(<A title="class in java.util" href="file:///C:/Program%20Files/Java/jdk1.5.0_02/docs/api/java/util/TimerTask.html">TimerTask</A>&nbsp;task,
                                long&nbsp;delay,
                                long&nbsp;period)</PRE>
<DL>
<DD>Schedules the specified task for repeated <I>fixed-rate execution</I>, beginning after the specified delay. Subsequent executions take place at approximately regular intervals, separated by the specified period. 
<P>In fixed-rate execution, each execution is scheduled relative to the scheduled execution time of the initial execution. If an execution is delayed for any reason (such as garbage collection or other background activity), two or more executions will occur in rapid succession to "catch up." In the long run, the frequency of execution will be exactly the reciprocal of the specified period (assuming the system clock underlying <TT>Object.wait(long)</TT> is accurate). 
<P>Fixed-rate execution is appropriate for recurring activities that are sensitive to <I>absolute</I> time, such as ringing a chime every hour on the hour, or running scheduled maintenance every day at a particular time. It is also appropriate for recurring activities where the total time to perform a fixed number of executions is important, such as a countdown timer that ticks once every second for ten seconds. Finally, fixed-rate execution is appropriate for scheduling multiple repeating timer tasks that must remain synchronized with respect to one another. 
<P></P>
<DD>
<DL>
<DT><B>Parameters:</B> 
<DD><CODE>task</CODE> - task to be scheduled. 
<DD><CODE>delay</CODE> - delay in milliseconds before task is to be executed. 
<DD><CODE>period</CODE> - time in milliseconds between successive task executions. 
<DT><B>Throws:</B> 
<DD><CODE><A title="class in java.lang" href="file:///C:/Program%20Files/Java/jdk1.5.0_02/docs/api/java/lang/IllegalArgumentException.html">IllegalArgumentException</A></CODE> - if <TT>delay</TT> is negative, or <TT>delay + System.currentTimeMillis()</TT> is negative. 
<DD><CODE><A title="class in java.lang" href="file:///C:/Program%20Files/Java/jdk1.5.0_02/docs/api/java/lang/IllegalStateException.html">IllegalStateException</A></CODE> - if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated.</DD></DL></DD></DL>说明：该方法和schedule的相同参数的版本类似，不同的是，如果该任务因为某些原因（例如垃圾收集）而延迟执行，那么接下来的任务会尽可能的快速执行，以赶上特定的时间点。<BR>
<HR>
<A name="scheduleAtFixedRate(java.util.TimerTask, java.util.Date, long)"><!-- --></A>
<H3>scheduleAtFixedRate</H3><PRE>public void <B>scheduleAtFixedRate</B>(<A title="class in java.util" href="file:///C:/Program%20Files/Java/jdk1.5.0_02/docs/api/java/util/TimerTask.html">TimerTask</A>&nbsp;task,
                                <A title="class in java.util" href="file:///C:/Program%20Files/Java/jdk1.5.0_02/docs/api/java/util/Date.html">Date</A>&nbsp;firstTime,
                                long&nbsp;period)</PRE>
<DL>
<DD>Schedules the specified task for repeated <I>fixed-rate execution</I>, beginning at the specified time. Subsequent executions take place at approximately regular intervals, separated by the specified period. 
<P>In fixed-rate execution, each execution is scheduled relative to the scheduled execution time of the initial execution. If an execution is delayed for any reason (such as garbage collection or other background activity), two or more executions will occur in rapid succession to "catch up." In the long run, the frequency of execution will be exactly the reciprocal of the specified period (assuming the system clock underlying <TT>Object.wait(long)</TT> is accurate). 
<P>Fixed-rate execution is appropriate for recurring activities that are sensitive to <I>absolute</I> time, such as ringing a chime every hour on the hour, or running scheduled maintenance every day at a particular time. It is also appropriate for recurring activities where the total time to perform a fixed number of executions is important, such as a countdown timer that ticks once every second for ten seconds. Finally, fixed-rate execution is appropriate for scheduling multiple repeating timer tasks that must remain synchronized with respect to one another. 
<P></P>
<DD>
<DL>
<DT><B>Parameters:</B> 
<DD><CODE>task</CODE> - task to be scheduled. 
<DD><CODE>firstTime</CODE> - First time at which task is to be executed. 
<DD><CODE>period</CODE> - time in milliseconds between successive task executions. 
<DT><B>Throws:</B> 
<DD><CODE><A title="class in java.lang" href="file:///C:/Program%20Files/Java/jdk1.5.0_02/docs/api/java/lang/IllegalArgumentException.html">IllegalArgumentException</A></CODE> - if <TT>time.getTime()</TT> is negative. 
<DD><CODE><A title="class in java.lang" href="file:///C:/Program%20Files/Java/jdk1.5.0_02/docs/api/java/lang/IllegalStateException.html">IllegalStateException</A></CODE> - if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated.<BR></DD></DL></DD></DL>说明：和上一个方法类似。<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;下面是我的一个测试片断：<BR><BR>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG id=Codehighlighter1_58_177_Open_Image onclick="this.style.display='none'; Codehighlighter1_58_177_Open_Text.style.display='none'; Codehighlighter1_58_177_Closed_Image.style.display='inline'; Codehighlighter1_58_177_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_58_177_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_58_177_Closed_Text.style.display='none'; Codehighlighter1_58_177_Open_Image.style.display='inline'; Codehighlighter1_58_177_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN style="COLOR: #000000">&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;main(String[]&nbsp;args)&nbsp;throws&nbsp;Exception&nbsp;</SPAN><SPAN id=Codehighlighter1_58_177_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_58_177_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;Timer&nbsp;timer&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Timer(</SPAN><SPAN style="COLOR: #0000ff">false</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;timer.schedule(</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Worker(),&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Date(System.currentTimeMillis()&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1000</SPAN><SPAN style="COLOR: #000000">));<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;&nbsp;}</SPAN></SPAN></DIV></DIV><img src ="http://www.blogjava.net/georgehill/aggbug/5793.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/georgehill/" target="_blank">小米</a> 2005-06-09 10:29 <a href="http://www.blogjava.net/georgehill/archive/2005/06/09/5793.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JavaMail的简单实例</title><link>http://www.blogjava.net/georgehill/archive/2005/06/02/5473.html</link><dc:creator>小米</dc:creator><author>小米</author><pubDate>Thu, 02 Jun 2005 08:30:00 GMT</pubDate><guid>http://www.blogjava.net/georgehill/archive/2005/06/02/5473.html</guid><wfw:comment>http://www.blogjava.net/georgehill/comments/5473.html</wfw:comment><comments>http://www.blogjava.net/georgehill/archive/2005/06/02/5473.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.blogjava.net/georgehill/comments/commentRss/5473.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/georgehill/services/trackbacks/5473.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;利用JavaMail的API可以快速的实现发送邮件的功能。下面是我使用的一个简单的实例，实现了简单的文本邮件的发送。import&nbsp;java.io.*;import&nbsp;java.util.*;import&nbsp;javax.activation.*;import&nbsp;javax.mail.*;...&nbsp;&nbsp;<a href='http://www.blogjava.net/georgehill/archive/2005/06/02/5473.html'>阅读全文</a><img src ="http://www.blogjava.net/georgehill/aggbug/5473.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/georgehill/" target="_blank">小米</a> 2005-06-02 16:30 <a href="http://www.blogjava.net/georgehill/archive/2005/06/02/5473.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>如何比较两个有可能为null的实例</title><link>http://www.blogjava.net/georgehill/archive/2005/05/25/5195.html</link><dc:creator>小米</dc:creator><author>小米</author><pubDate>Wed, 25 May 2005 09:00:00 GMT</pubDate><guid>http://www.blogjava.net/georgehill/archive/2005/05/25/5195.html</guid><wfw:comment>http://www.blogjava.net/georgehill/comments/5195.html</wfw:comment><comments>http://www.blogjava.net/georgehill/archive/2005/05/25/5195.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/georgehill/comments/commentRss/5195.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/georgehill/services/trackbacks/5195.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;以前在写程序的时候，碰到需要比较两个有可能为null的实例时，为了避免出现NullPointerException，经常用这样的一段代码来比较：<BR><BR>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;Object&nbsp;obj1&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">abc</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;Object&nbsp;obj2&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">cde</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;((obj1&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;obj2&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #000000">||</SPAN><SPAN style="COLOR: #000000">&nbsp;(obj1&nbsp;</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;obj1.equals(obj2))&nbsp;<BR><IMG id=Codehighlighter1_182_223_Open_Image onclick="this.style.display='none'; Codehighlighter1_182_223_Open_Text.style.display='none'; Codehighlighter1_182_223_Closed_Image.style.display='inline'; Codehighlighter1_182_223_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_182_223_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_182_223_Closed_Text.style.display='none'; Codehighlighter1_182_223_Open_Image.style.display='inline'; Codehighlighter1_182_223_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">||</SPAN><SPAN style="COLOR: #000000">&nbsp;(obj2&nbsp;</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;obj2.equals(obj1)))&nbsp;</SPAN><SPAN id=Codehighlighter1_182_223_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_182_223_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.</SPAN><SPAN style="COLOR: #0000ff">out</SPAN><SPAN style="COLOR: #000000">.println(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">equals</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN></DIV></DIV><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;这样的程序，读起来真是挺拗口。我一直没有想到什么好的方法解决这个问题，直到今天在看到JDK的AbstractList源码的equals方法的实现时，看到这一段：<BR><BR>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #000000">!</SPAN><SPAN style="COLOR: #000000">(o1</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">?</SPAN><SPAN style="COLOR: #000000">&nbsp;o2</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">&nbsp;:&nbsp;o1.equals(o2)))<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">false</SPAN><SPAN style="COLOR: #000000">;</SPAN></DIV></DIV><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;原来用三元运算符可以很好的解决这个问题，呵呵，我前面的程序可以改写成：<BR><BR>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;Object&nbsp;obj1&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">abc</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;Object&nbsp;obj2&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">cde</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;(obj1&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">?</SPAN><SPAN style="COLOR: #000000">&nbsp;obj2&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">&nbsp;:&nbsp;obj1.equals(obj2))<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.</SPAN><SPAN style="COLOR: #0000ff">out</SPAN><SPAN style="COLOR: #000000">.println(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">equals</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);</SPAN></DIV></DIV><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;真是简洁多了！<IMG height=20 src="http://www.blogjava.net/Emoticons/QQ/05.gif" width=20 border=0><img src ="http://www.blogjava.net/georgehill/aggbug/5195.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/georgehill/" target="_blank">小米</a> 2005-05-25 17:00 <a href="http://www.blogjava.net/georgehill/archive/2005/05/25/5195.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>从SocketChannel对象池中获取的实例，使用时应注意的问题</title><link>http://www.blogjava.net/georgehill/archive/2005/05/25/5180.html</link><dc:creator>小米</dc:creator><author>小米</author><pubDate>Wed, 25 May 2005 07:02:00 GMT</pubDate><guid>http://www.blogjava.net/georgehill/archive/2005/05/25/5180.html</guid><wfw:comment>http://www.blogjava.net/georgehill/comments/5180.html</wfw:comment><comments>http://www.blogjava.net/georgehill/archive/2005/05/25/5180.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/georgehill/comments/commentRss/5180.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/georgehill/services/trackbacks/5180.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;从对象池中获取的实例，因为并不清楚该channel是否已经设置成正确的状态，所以在使用时最好重新设置一遍。有以下几点需要注意：<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1.在使用阻塞IO时，需要把该channel设置成阻塞的，即需要调用SocketChannel.configureBlocking(true);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2.在使用非阻塞IO时，需要把该channel设置成非阻塞的，即需要调用SocketChannel.configureBlocking(false);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.如果该channel注册了selector，那么在返回该实例到对象池中，需要把注册的selector清除，即需要调用Selector的close方法。<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;下面是一段应用场景的例子：<BR><BR>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/dot.gif"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;把命令输出</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;channel.configureBlocking(</SPAN><SPAN style="COLOR: #0000ff">true</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintWriter&nbsp;writer&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;PrintWriter(channel.socket().getOutputStream(),&nbsp;</SPAN><SPAN style="COLOR: #0000ff">false</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.write(command.endsWith(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #000000">?</SPAN><SPAN style="COLOR: #000000">&nbsp;command&nbsp;:&nbsp;command&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.flush();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;channel.configureBlocking(</SPAN><SPAN style="COLOR: #0000ff">false</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;创建Selector</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Selector&nbsp;selector&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;Selector.open();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;向Selector注册我们需要的READ事件</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SelectionKey&nbsp;skey&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;channel.register(selector,&nbsp;SelectionKey.OP_READ);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;boolean&nbsp;stop&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">false</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;n&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;read&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ByteBuffer&nbsp;buffer&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;ByteBuffer.allocate(</SPAN><SPAN style="COLOR: #000000">1024</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;轮询</SPAN><SPAN style="COLOR: #008000"><BR><IMG id=Codehighlighter1_637_809_Open_Image onclick="this.style.display='none'; Codehighlighter1_637_809_Open_Text.style.display='none'; Codehighlighter1_637_809_Closed_Image.style.display='inline'; Codehighlighter1_637_809_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_637_809_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_637_809_Closed_Text.style.display='none'; Codehighlighter1_637_809_Open_Image.style.display='inline'; Codehighlighter1_637_809_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">while</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #000000">!</SPAN><SPAN style="COLOR: #000000">stop)&nbsp;</SPAN><SPAN id=Codehighlighter1_637_809_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_637_809_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;获取Selector返回的时间值</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;selector.select();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;当传回的值大于0事，读事件发生了</SPAN><SPAN style="COLOR: #008000"><BR><IMG id=Codehighlighter1_754_799_Open_Image onclick="this.style.display='none'; Codehighlighter1_754_799_Open_Text.style.display='none'; Codehighlighter1_754_799_Closed_Image.style.display='inline'; Codehighlighter1_754_799_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_754_799_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_754_799_Closed_Text.style.display='none'; Codehighlighter1_754_799_Open_Image.style.display='inline'; Codehighlighter1_754_799_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;(n&nbsp;</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN id=Codehighlighter1_754_799_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_754_799_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;处理发生的事件</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<IMG src="http://www.blogjava.net/images/dot.gif"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selector.close();</SPAN></DIV></DIV><img src ="http://www.blogjava.net/georgehill/aggbug/5180.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/georgehill/" target="_blank">小米</a> 2005-05-25 15:02 <a href="http://www.blogjava.net/georgehill/archive/2005/05/25/5180.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JDK Dynamic Proxy模式的简单范例</title><link>http://www.blogjava.net/georgehill/archive/2005/05/24/5126.html</link><dc:creator>小米</dc:creator><author>小米</author><pubDate>Tue, 24 May 2005 09:47:00 GMT</pubDate><guid>http://www.blogjava.net/georgehill/archive/2005/05/24/5126.html</guid><wfw:comment>http://www.blogjava.net/georgehill/comments/5126.html</wfw:comment><comments>http://www.blogjava.net/georgehill/archive/2005/05/24/5126.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.blogjava.net/georgehill/comments/commentRss/5126.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/georgehill/services/trackbacks/5126.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;在JDK1.3版本中引入了Dynamic Proxy的代理机制，通过实现java.lang.reflect.InvocationHandler接口，可以实现拦截需要改写的方法。下面是一个简单范例。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;有下面一个接口TestInterface和它的一个实现TestImpl：<BR><BR>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">package&nbsp;sample.proxy;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG id=Codehighlighter1_23_182_Open_Image onclick="this.style.display='none'; Codehighlighter1_23_182_Open_Text.style.display='none'; Codehighlighter1_23_182_Closed_Image.style.display='inline'; Codehighlighter1_23_182_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_23_182_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_23_182_Closed_Text.style.display='none'; Codehighlighter1_23_182_Open_Image.style.display='inline'; Codehighlighter1_23_182_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_23_182_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_23_182_Open_Text><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">*<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;&lt;p&gt;Title:&nbsp;&lt;/p&gt;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;&lt;p&gt;Description:&nbsp;&lt;/p&gt;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;&lt;p&gt;Copyright:&nbsp;Copyright&nbsp;(c)&nbsp;2005&lt;/p&gt;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;&lt;p&gt;Company:&nbsp;&lt;/p&gt;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;@author&nbsp;George&nbsp;Hill<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;@version&nbsp;1.0<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG id=Codehighlighter1_216_245_Open_Image onclick="this.style.display='none'; Codehighlighter1_216_245_Open_Text.style.display='none'; Codehighlighter1_216_245_Closed_Image.style.display='inline'; Codehighlighter1_216_245_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_216_245_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_216_245_Closed_Text.style.display='none'; Codehighlighter1_216_245_Open_Image.style.display='inline'; Codehighlighter1_216_245_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">interface</SPAN><SPAN style="COLOR: #000000">&nbsp;TestInterface&nbsp;</SPAN><SPAN id=Codehighlighter1_216_245_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_216_245_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;String&nbsp;print();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></DIV></DIV><BR>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">package&nbsp;sample.proxy;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG id=Codehighlighter1_23_182_Open_Image onclick="this.style.display='none'; Codehighlighter1_23_182_Open_Text.style.display='none'; Codehighlighter1_23_182_Closed_Image.style.display='inline'; Codehighlighter1_23_182_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_23_182_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_23_182_Closed_Text.style.display='none'; Codehighlighter1_23_182_Open_Image.style.display='inline'; Codehighlighter1_23_182_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_23_182_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_23_182_Open_Text><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">*<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;&lt;p&gt;Title:&nbsp;&lt;/p&gt;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;&lt;p&gt;Description:&nbsp;&lt;/p&gt;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;&lt;p&gt;Copyright:&nbsp;Copyright&nbsp;(c)&nbsp;2005&lt;/p&gt;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;&lt;p&gt;Company:&nbsp;&lt;/p&gt;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;@author&nbsp;George&nbsp;Hill<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;@version&nbsp;1.0<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG id=Codehighlighter1_232_316_Open_Image onclick="this.style.display='none'; Codehighlighter1_232_316_Open_Text.style.display='none'; Codehighlighter1_232_316_Closed_Image.style.display='inline'; Codehighlighter1_232_316_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_232_316_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_232_316_Closed_Text.style.display='none'; Codehighlighter1_232_316_Open_Image.style.display='inline'; Codehighlighter1_232_316_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;TestImpl&nbsp;implements&nbsp;TestInterface&nbsp;</SPAN><SPAN id=Codehighlighter1_232_316_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_232_316_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;<BR><IMG id=Codehighlighter1_261_311_Open_Image onclick="this.style.display='none'; Codehighlighter1_261_311_Open_Text.style.display='none'; Codehighlighter1_261_311_Closed_Image.style.display='inline'; Codehighlighter1_261_311_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_261_311_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_261_311_Closed_Text.style.display='none'; Codehighlighter1_261_311_Open_Image.style.display='inline'; Codehighlighter1_261_311_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;String&nbsp;print()&nbsp;</SPAN><SPAN id=Codehighlighter1_261_311_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_261_311_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Hello,&nbsp;it's&nbsp;from&nbsp;TestImpl&nbsp;class</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></DIV></DIV><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;下面拦截print方法，调用自己的实现，这需要实现java.lang.reflect.InvocationHandler接口。<BR><BR>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">package&nbsp;sample.proxy;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>import&nbsp;java.lang.reflect.</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG id=Codehighlighter1_52_211_Open_Image onclick="this.style.display='none'; Codehighlighter1_52_211_Open_Text.style.display='none'; Codehighlighter1_52_211_Closed_Image.style.display='inline'; Codehighlighter1_52_211_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_52_211_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_52_211_Closed_Text.style.display='none'; Codehighlighter1_52_211_Open_Image.style.display='inline'; Codehighlighter1_52_211_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_52_211_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_52_211_Open_Text><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">*<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;&lt;p&gt;Title:&nbsp;&lt;/p&gt;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;&lt;p&gt;Description:&nbsp;&lt;/p&gt;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;&lt;p&gt;Copyright:&nbsp;Copyright&nbsp;(c)&nbsp;2005&lt;/p&gt;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;&lt;p&gt;Company:&nbsp;&lt;/p&gt;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;@author&nbsp;George&nbsp;Hill<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;@version&nbsp;1.0<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG id=Codehighlighter1_268_1110_Open_Image onclick="this.style.display='none'; Codehighlighter1_268_1110_Open_Text.style.display='none'; Codehighlighter1_268_1110_Closed_Image.style.display='inline'; Codehighlighter1_268_1110_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_268_1110_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_268_1110_Closed_Text.style.display='none'; Codehighlighter1_268_1110_Open_Image.style.display='inline'; Codehighlighter1_268_1110_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;TestHandler&nbsp;implements&nbsp;InvocationHandler&nbsp;</SPAN><SPAN id=Codehighlighter1_268_1110_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_268_1110_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;TestInterface&nbsp;test;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;<BR><IMG id=Codehighlighter1_300_416_Open_Image onclick="this.style.display='none'; Codehighlighter1_300_416_Open_Text.style.display='none'; Codehighlighter1_300_416_Closed_Image.style.display='inline'; Codehighlighter1_300_416_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_300_416_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_300_416_Closed_Text.style.display='none'; Codehighlighter1_300_416_Open_Image.style.display='inline'; Codehighlighter1_300_416_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_300_416_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_300_416_Open_Text><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">*<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;将动态代理绑定到指定的TestInterface<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;test&nbsp;TestInterface<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;TestInterface&nbsp;绑定代理后的TestInterface<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG id=Codehighlighter1_466_674_Open_Image onclick="this.style.display='none'; Codehighlighter1_466_674_Open_Text.style.display='none'; Codehighlighter1_466_674_Closed_Image.style.display='inline'; Codehighlighter1_466_674_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_466_674_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_466_674_Closed_Text.style.display='none'; Codehighlighter1_466_674_Open_Image.style.display='inline'; Codehighlighter1_466_674_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;TestInterface&nbsp;bind(TestInterface&nbsp;test)&nbsp;</SPAN><SPAN id=Codehighlighter1_466_674_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_466_674_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.test&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;test;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;TestInterface&nbsp;proxyTest&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(TestInterface)&nbsp;Proxy.newProxyInstance(<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;test.getClass().getClassLoader(),&nbsp;test.getClass().getInterfaces(),&nbsp;</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;proxyTest;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;<BR><IMG id=Codehighlighter1_681_832_Open_Image onclick="this.style.display='none'; Codehighlighter1_681_832_Open_Text.style.display='none'; Codehighlighter1_681_832_Closed_Image.style.display='inline'; Codehighlighter1_681_832_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_681_832_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_681_832_Closed_Text.style.display='none'; Codehighlighter1_681_832_Open_Image.style.display='inline'; Codehighlighter1_681_832_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_681_832_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_681_832_Open_Text><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">*<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;方法调用拦截器，拦截print方法<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;proxy&nbsp;Object<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;method&nbsp;Method<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;args&nbsp;Object[]<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;Object<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;@throws&nbsp;Throwable<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG id=Codehighlighter1_918_1105_Open_Image onclick="this.style.display='none'; Codehighlighter1_918_1105_Open_Text.style.display='none'; Codehighlighter1_918_1105_Closed_Image.style.display='inline'; Codehighlighter1_918_1105_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_918_1105_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_918_1105_Closed_Text.style.display='none'; Codehighlighter1_918_1105_Open_Image.style.display='inline'; Codehighlighter1_918_1105_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;Object&nbsp;invoke(Object&nbsp;proxy,&nbsp;Method&nbsp;method,&nbsp;Object[]&nbsp;args)&nbsp;throws&nbsp;Throwable&nbsp;</SPAN><SPAN id=Codehighlighter1_918_1105_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_918_1105_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;如果调用的是print方法，则替换掉</SPAN><SPAN style="COLOR: #008000"><BR><IMG id=Codehighlighter1_988_1043_Open_Image onclick="this.style.display='none'; Codehighlighter1_988_1043_Open_Text.style.display='none'; Codehighlighter1_988_1043_Closed_Image.style.display='inline'; Codehighlighter1_988_1043_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_988_1043_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_988_1043_Closed_Text.style.display='none'; Codehighlighter1_988_1043_Open_Image.style.display='inline'; Codehighlighter1_988_1043_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">print</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">.equals(method.getName()))&nbsp;</SPAN><SPAN id=Codehighlighter1_988_1043_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_988_1043_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">HaHa,&nbsp;It's&nbsp;come&nbsp;from&nbsp;TestHandler</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR><IMG id=Codehighlighter1_1050_1101_Open_Image onclick="this.style.display='none'; Codehighlighter1_1050_1101_Open_Text.style.display='none'; Codehighlighter1_1050_1101_Closed_Image.style.display='inline'; Codehighlighter1_1050_1101_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_1050_1101_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1050_1101_Closed_Text.style.display='none'; Codehighlighter1_1050_1101_Open_Image.style.display='inline'; Codehighlighter1_1050_1101_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">else</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN id=Codehighlighter1_1050_1101_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_1050_1101_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;method.invoke(</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.test,&nbsp;args);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></DIV></DIV><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;下面是测试用例：<BR><BR>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">package&nbsp;sample.test;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>import&nbsp;junit.framework.</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>import&nbsp;sample.proxy.</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG id=Codehighlighter1_73_239_Open_Image onclick="this.style.display='none'; Codehighlighter1_73_239_Open_Text.style.display='none'; Codehighlighter1_73_239_Closed_Image.style.display='inline'; Codehighlighter1_73_239_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_73_239_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_73_239_Closed_Text.style.display='none'; Codehighlighter1_73_239_Open_Image.style.display='inline'; Codehighlighter1_73_239_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_73_239_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_73_239_Open_Text><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">*<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;&lt;p&gt;Title:&nbsp;&lt;/p&gt;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;&lt;p&gt;Description:&nbsp;&lt;/p&gt;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;&lt;p&gt;Copyright:&nbsp;Copyright&nbsp;(c)&nbsp;2005&lt;/p&gt;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;&lt;p&gt;Company:&nbsp;&lt;/p&gt;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;@author&nbsp;George&nbsp;Hill<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;@version&nbsp;1.0<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG id=Codehighlighter1_289_669_Open_Image onclick="this.style.display='none'; Codehighlighter1_289_669_Open_Text.style.display='none'; Codehighlighter1_289_669_Closed_Image.style.display='inline'; Codehighlighter1_289_669_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_289_669_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_289_669_Closed_Text.style.display='none'; Codehighlighter1_289_669_Open_Image.style.display='inline'; Codehighlighter1_289_669_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;TestDynamicProxy&nbsp;extends&nbsp;TestCase&nbsp;</SPAN><SPAN id=Codehighlighter1_289_669_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_289_669_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;TestInterface&nbsp;test&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</