服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > 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 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).

扫描关注微信公众号