制作java应用程序的帮助文件

    技术2022-05-11  137

    如果你使用过JBuilder,并且看过它的帮助的话,想不想让你自己的应用程序也有个这么专业的帮助呢?事实上做起来是很简单的,sun提供了一个JavaHelp可以帮助我们实现这个目的。使用javaHelp为应用添加帮助有两个步骤:1,制作帮助文件(helpSet)。现在先让我们来制作helpSet吧,打开http://java.sun.com/products/javahelp/download_binary.html. 下载一个zip版本的JavaHelp,目前最高好像是1.1.3。开始制作一个名为"Hello,JavaHelp"的HelpSet,创建一个目录:"help",help下面再创建一个目录"hello"在hello目录下面创建两个目录"First","Last",结构如下:+ help+ Hello+ First+ Last目录建好了以后,我们要在Help目录下面写出四个文件:hello.hs,Map.jhm,index.xml以及toc.xmlhello.hs:<?xml version='1.0' encoding='ISO-8859-1' ?><!DOCTYPE helpsetPUBLIC "-//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><view><name>Index</name><label>Index</label><type>javax.help.IndexView</type><data>index.xml</data></view></helpset>下面是Map.jhm文件的内容:<?xml version='1.0' encoding='ISO-8859-1' ?><!DOCTYPE mapPUBLIC "-//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>下面是index.xml文件的内容:<?xml version='1.0' encoding='ISO-8859-1' ?><!DOCTYPE indexPUBLIC "-//Sun Microsystems Inc.//DTD JavaHelp Index Version 1.0//EN""http://java.sun.com/products/javahelp/index_1_0.dtd"><index version="1.0"><indexitem text="The First?"><indexitem target="one" text="I'm One"/><indexitem target="two" text="I'm Second"/></indexitem><indexitem text="The Last?"><indexitem target="three" text="We're Third!"/><indexitem target="four" text="We're Last"/></indexitem><indexitem target="overview" text="Overview!!!"/></index>下面是toc.xml文件的内容:<?xml version='1.0' encoding='ISO-8859-1' ?><!DOCTYPE tocPUBLIC "-//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 image="toplevelfolder" 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>创建好以上四个文件以后,记得把它们放到help目录下面,现在需要四个htm文件,把帮助内容写在htm里面,Hello/overview.htmHello/First/one.htmHello/First/two.htmHello/Last/three.htmHello/Last/four.htm那么现在Help目录下的结构就变成了下面一样:+ helphello.hsindex.xmlMap.jhmtoc.xml+ Hellooverview.htm+ Firstone.htmtwo.htm+ Lastthree.htmfour.htm还记得开始让你下载的JavaHelp吗?解包以后在jh1.1.3/demos/bin目录下面有个hsviewer.jar文件我们用它来查看帮助文件是否做得完美,将hsviewer.jar加入到classpath里面假如jh1.1.3解压到了E:/,set classpath=%classpath%;E:/jh1.1.3/demos/bin/hsviewer.jar然后执行:java sunw.demo.jhdemo.JHLauncher有一个图形界面出来,按浏览按钮找到Help/hello.hs文件,按display显示帮助文件。helpset显示如下图:

    2,将帮助加到你的应用中来。本质上,将应用程序和HelpSet联系起来的方法就是将帮助文件名映射到swing的组件里来。这时你需要jh.jar这个文件在jh1.1.3/javahelp/lib里面,你必须把它加入classpath或者是拷贝到jre/lib/ext目录下。如果你用JBuilder,可以在菜单Tools的Configure Libraries里面增加一个Lib,选择jh.jar,然后在Project里面选择使用这个Lib。javahelp的lib不小,其实核心的三个类HelpSet, HelpBroker,和CSH就可以使javahelp运行起来了。首先导入javahelp的类:import javax.help.*;然后你得找到HelpSet文件,通过包含HelpSet的URL对象或者使用HelpSet类的findHelpSet方法得到URL对象,findHelpSet方法通过ClassLoader找到帮助文件。有了URL对象以后就可以构造一个HelpSet对象了:import java.net.*;...HelpSet helpset = null;ClassLoader loader = null;URL url = HelpSet.findHelpSet(loader, "hello.hs");try {helpset = new HelpSet(loader, url);} catch (HelpSetException e) {System.err.println("Error loading");return;}然后你要从helpset得到HelpBroker对象 。HelpBroker helpbroker = helpset.createHelpBroker();最后是帮助跟组件的绑定。ActionListener listener = new CSH.DisplayHelpFromSource(helpbroker);overview.addActionListener(listener);完整代码如下:import javax.swing.*;import java.awt.*;import java.awt.event.*;import javax.help.*;import java.net.*;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");JMenuItem specific = new JMenuItem("Specific");helpMenu.add(overview);helpMenu.add(specific);menubar.add(helpMenu);frame.setJMenuBar(menubar);JButton button1 = new JButton("The Button");JButton button2 = new JButton("Context");content.add(button1, BorderLayout.NORTH);content.add(button2, BorderLayout.SOUTH);HelpSet helpset = null;ClassLoader loader = null;URL url = HelpSet.findHelpSet(loader, "hello.hs"); 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);CSH.setHelpIDString(specific, "one");specific.addActionListener(listener);CSH.setHelpIDString(button1, "two");ActionListener tracker = new CSH.DisplayHelpAfterTracking(helpbroker);button2.addActionListener(tracker);JRootPane rootpane = frame.getRootPane();helpbroker.enableHelpKey(rootpane, "three", helpset);frame.setSize(200, 200);frame.show();}}

    以上目录和文件以及程序你可以在http://www.jzventures.com/javahelp.zip下载。


    最新回复(0)