Java編程小技巧集錦

 


Java編程小技巧集錦

1. Javadoc API文件產生器
Javadoc
程序讀取一個Java類文件並自動創建一組HTML文件,這些HTML 文件描述了Java類文件的類、變量、成員函數,所有Java類庫的APIHTML 文件都可以由此程序創建。Javadoc把軟件包名或源文件列表當做一個變量。Javadoc依靠以@打頭的備註標記來創建HTML文件,下面就是標註的列表,它們被Javadoc用於在HTML 文件中創建鏈接。

選項 功能
see classname 此標註在類列表中增加一個到所提供類的"See Also"條目。
see classname # methodname 此標註創建一個到特定的成員函數的"See Also"條目。
version text 此標註在HTML文件中加入一個版本信息條目
author text 此標註在HTML文件中加入一個作者信息條目
param name description 此標註用成員函數備註來描述一個成員函數所帶變量
return description 此標註用成員函數備註來描述返回值
exception classname 此標註用成員函數備註來連接成員函數產生的異常出口
-classpath path
此命令行指定尋找Java文件的目錄
-d directory
此命令行指定用來放入最終HTML文件十分有用。



2
調試器--jdb.exe

Java
調度器爲Java程序提供了一個命令行調試環境。它既可在本地,也可在與遠程的解釋器的一次對話中執行。jdb於本地機器中可用如下的命令啓動。

選項 功能
catch calssID
爲特定異常出口而中斷
classes
列出當前已知的類
clear classID:line
清除一個斷點
cont
從斷點處繼續執行
down[n frames]
下移一個線程的堆棧
dump ID[ID...]
顯示所有對象信息
exit(
quit) 退出調試器
help(
?) 列出所有命令
ignore classID
忽略特定的異常出口
list[line number]
顯示源代碼
load classbame
載入要調試的Java
locals
在當前堆棧幀中顯示所有局部變量
memory
報告內存使用情況
methods classID
列出一個類的成員函數集
print ID[ID...]
列出對象或域
resume [threadID...]
恢復線程(默認情況恢復所有線程)
run class [args]
開始執行已下載的Java
step
執行當前行
stop in classID:method
在一成員函數中設一斷點
stop at classID:line
在一行設一斷點
suspend[threadID...]
停止一個線程(默認情況停止所有線程)
hreads threadgroup
列出線程
thread threadID
設置當前線程
threadgroups
列出線程組
threadgroup name
設置當前線程組
up [n frames]
上移一個線程堆棧
use [path]
顯示或改變源程序路徑
where [threadID] or all
使一線程的堆線置空
!!
重複上一次命令
-host hostname
該命令告訴Jdb到哪裏去建立遠程運行的Java解釋器對話過程
-password password
本選項告訴Jdb 用哪個密碼去與遠程運行的Java 對話進程相連接。

密碼 password是由運行帶有-debug選項的Java解釋器所提供的。



3
Applet中引用jar中的資源文件

如果想在servlets程序設計中加上一些圖片,聲音,卡通等,只需使用sun 公司提供的一個有用的工具:jar。這個工具可以把這些資源文件合在一個文件裏,避免頻繁的http request,可以下載緩存!

jar中的資源的實例方法如下:加一個圖片按扭ImageButton

(提個醒i.e.g :聲音,卡通,圖片相對路徑爲./img/my.gif

import java.awt.*;
import java.awt.event.*; //
下載吧
import javax.swing.*; //
下載吧
public class ImageButtonApplet extends JApplet{
private String path = "/img/my.gif";
private ImageIcon myButtonIcon = new ImageIcon(getClass().getResource(path));

/*
通過本人多次調試和看jdk自帶的demo 自代的API 文擋, JDK1.1得來,相關還有ClassLoader demo在引用資源的時候採用方法 getClass().getResource(String sourceName)

如下:

public URL getResource(String name)
Finds a resource with a given name. This method returns null if no resource with this name is found. The rules for searching resources associated with a given class are implemented by the * defining class loader of the class.
This method delegates the call to its class loader, after making these changes to the resource name: if the resource name starts with "/", it is unchanged; otherwise, the package name is prepended to the resource name after converting "." to "/". If this object was loaded by the bootstrap loader, the call is delegated to ClassLoader.getSystemResource.
Parameters:
name - name of the desired resource
Returns:
a java.net.URL object.

*/
/**Initialize the applet*/
public void init(){
try {
if (myButtonIcon == null)
throw new Exception("cannot get the p_w_picpath!");
JButton iButton = new JButton(myButtonIcon);
Container cp = this.getContentPane();
cp.add(iButton);
}
catch (Exception e){
e.printStackTrace();
}
}

}

子編譯之後,把ImageButtonApplet.classmy.gif保持相對路徑打進jar裏面,對應的HTML頁面代碼爲。成功關鍵在於使用getClass().getResource(path). http://wenku.it168.com/d_000068666.shtml

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章