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

如果你使用过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.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>
<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 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>
下面是index.xml文件的内容:
<?xml version='1.0' encoding='ISO-8859-1' ?>
<!DOCTYPE index
PUBLIC "-//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 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 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.htm
Hello/First/one.htm
Hello/First/two.htm
Hello/Last/three.htm
Hello/Last/four.htm
那么现在Help目录下的结构就变成了下面一样:
+ help
hello.hs
index.xml
Map.jhm
toc.xml
+ Hello
overview.htm
+ First
one.htm
two.htm
+ Last
three.htm
four.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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值