使用javaHelp制作java swing帮助文档

 

最近在做一个Swing项目,项目接近尾声,需要做最后的帮助系统了。简单搜索了一下,找到了javaHelp。JavaHelp是sun推出的编写帮助系统的一个类库,能够开发类似windows的CHM帮助文档。Jar包下载地址:http://dl.vmall.com/c0paz9lpea 。

一个完整的帮助系统至少需要下图中的几个文件。HelloHelp.java是帮助系统启动的类。Hello.hs是帮助系统的核心helpset,必须以hs为后缀。Map.jhm是一个映射文件,它将帮助系统中每一项映射到硬盘上的某个html文件,必须以jhm结尾。Toc.xml是主题文件,可以控制帮助系统的界面等一些东西。Hello文件夹内是N多个html文件,和CHM类似,这个不多说。下面详细解释一下每个文件。

使用javaHelp制作java swing帮助文档

 

  1. hello.java
public class HelloHelp {



  public static void main(String args[]) {



    JFrame frame = new JFrame("Hello, JavaHelp");



    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    Container content = frame.getContentPane();



 



    JMenuBar menubar = new JMenuBar();



    JMenu helpMenu = new JMenu("Help");



    JMenuItem overview = new JMenuItem("Overview");



    helpMenu.add(overview);



    menubar.add(helpMenu);



    frame.setJMenuBar(menubar);



 



    HelpSet helpset = null;



    ClassLoader loader = null;



    URL url = HelpSet.findHelpSet(loader, "hello.hs");//这一句是找到helpset文件



    try {



      helpset = new HelpSet(loader, url);



    } catch (HelpSetException e) {



      System.err.println("Error loading");



      return;



    }



    HelpBroker helpbroker = helpset.createHelpBroker();



    ActionListener listener =



      new CSH.DisplayHelpFromSource(helpbroker);



    overview.addActionListener(listener);



 



    frame.setSize(500, 300);



    frame.show();



  }



}

 

2.接下来看Hello.hs中的代码。一个maps节点指向的是map映射文件<homeID>overview</homeID>这一句指定了帮助系统的根节点,其中view节点指向的是主题toc.xml文件                                                                                                                                                                                                         

//////////Hello.hs源码/////////////

<?xml version='1.0' encoding='ISO-8859-1' ?>



     <!DOCTYPE helpset



       PUBLIC "-//Sun Microsystems Inc.//DTD JavaHelp HelpSet Version 1.0//EN"



          "http://java.sun.com/products/javahelp/helpset_1_0.dtd">  



     <helpset version="1.0">



     <title>Hello, JavaHelp</title>



    <maps>



      <mapref location="Map.jhm"/>



        <homeID>overview</homeID>



      </maps>



    <view>



    <name>TOC</name>



    <label>TOC</label>



    <type>javax.help.TOCView</type>



    <data>toc.xml</data>



  </view>



</helpset>

 

 

3.继续看map映射文件。MapID的target是在其他代码中使用的,不能重复。url指向的就是html文件。帮助系统中的每个帮助项都是一个html文件。

//////////map.jhm源码/////////////

 

<?xml version='1.0' encoding='ISO-8859-1' ?>



<!DOCTYPE map



  PUBLIC "-//Sun Microsystems Inc.//DTD JavaHelp Map Version 1.0//EN"



         "http://java.sun.com/products/javahelp/map_1_0.dtd">



 



<map version="1.0">



  <mapID target="overview" url="Hello/overview.htm" />



  <mapID target="one" url="Hello/First/one.htm" />



  <mapID target="two" url="Hello/First/two.htm" />



  <mapID target="three" url="Hello/Last/three.htm" />



  <mapID target="four" url="Hello/Last/four.htm" />



</map>

 

4.最后是toc.xml。target必须是在map中已定义的值,不然是找不到的。像这种结构

 

<tocitem target="overview" text="Hello, JavaHelp">



    <tocitem text="First Stuff">



      <tocitem target="one" text="The One"/>



      <tocitem target="two" text="The Second"/>



         </tocitem>



</tocitem>

 

最后显示出来的结构就是这样的

--- Hello, JavaHelp

   ----- First Stuff

       ----- The One

       ----- The Second

Text是在界面上显示的值

//////////toc.xml源码/////////////

 

<?xml version='1.0' encoding='ISO-8859-1' ?>



<!DOCTYPE toc



  PUBLIC "-//Sun Microsystems Inc.//DTD JavaHelp TOC Version 1.0//EN"



         "http://java.sun.com/products/javahelp/toc_1_0.dtd">



 



<toc version="1.0">



<tocitem target="overview" text="Hello, JavaHelp">



    <tocitem text="First Stuff">



      <tocitem target="one" text="The One"/>



      <tocitem target="two" text="The Second"/>



    </tocitem>



    <tocitem text="Last Stuff">



      <tocitem target="three" text="What's Third?"/>



      <tocitem target="four" text="The End"/>



    </tocitem>



</tocitem>



</toc>

 

 

这个比较简单,有兴趣的可以把代码复制进自己的程序试试。记得把Hello文件中的几个html文件补上。

转载请注明:http://www.cnblogs.com/timeng/archive/2012/08/16/2643241.html

你可能感兴趣的:(java swing)