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 下移一个线程的堆栈
dump ID 显示所有对象信息
exit(或quit) 退出调试器
help(或?) 列出所有命令
ignore classID 忽略特定的异常出口
list 显示源代码
load classbame 载入要调试的Java类
locals 在当前堆栈帧中显示所有局部变量
memory 报告内存使用情况
methods classID 列出一个类的成员函数集
print ID 列出对象或域
resume 恢复线程(默认情况恢复所有线程)
run class 开始执行已下载的Java类
step 执行当前行
stop in classID:method 在一成员函数中设一断点
stop at classID:line 在一行设一断点
suspend 停止一个线程(默认情况停止所有线程)
hreads threadgroup 列出线程
thread threadID 设置当前线程
threadgroups 列出线程组
threadgroup name 设置当前线程组
up 上移一个线程堆栈
use 显示或改变源程序路径
where 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 image!");
JButton iButton = new JButton(myButtonIcon);
Container cp = this.getContentPane();
cp.add(iButton);
}
catch (Exception e){
e.printStackTrace();
}
}
}
子编译之后,把ImageButtonApplet.class和my.gif保持相对路径打进jar里面,对应的HTML页面代码为。成功关键在于使用getClass().getResource(path).
页:
[1]