将按钮菜单添加到javahelp

时间:2018-09-27 13:23:08

标签: java swing javahelp

在我的程序中,我将javahelp与helpbroker一起使用:窗口的所有摆动组件都是自动生成和定位的。

我想自定义帮助经纪人,如图所示在窗口底部添加一个按钮栏。

enter image description here

最简单的方法是什么?

谢谢

1 个答案:

答案 0 :(得分:0)

添加帮助按钮的唯一方法是将javahelp嵌入JFrame中:

public class vmHelp {
public static void main(String args[]) {
    JHelp helpViewer = null;
    String title = "";
    try {
        // Get the classloader of this class.                                                                                                                                                             
        ClassLoader cl = vmHelp.class.getClassLoader();
        // Use the findHelpSet method of HelpSet to create a URL referencing the helpset file.                                                                                                            
        // Note that in this example the location of the helpset is implied as being in the same                                                                                                          
        // directory as the program by specifying "jhelpset.hs" without any directory prefix,                                                                                                             
        // this should be adjusted to suit the implementation.                                                                                                                                            
        String lHelpSetFile = "APP.hs";
        URL url = HelpSet.findHelpSet(cl, lHelpSetFile);
        if (url == null) {
            System.err.println("URL is null, maybe the help set file is wrong: " + lHelpSetFile + ". Look at vmHelp.java");
            return;
        }
        // Create a new JHelp object with a new HelpSet.                                                                                                                                                  
        HelpSet h = new HelpSet(cl, url);
        title = h.getTitle();
        helpViewer = new JHelp(h);
        // Set the initial entry point in the table of contents.                                                                                                                                          
        helpViewer.setCurrentID("top");
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }

    // Create a new frame.                                                                                                                                                                                
    JFrame frame = new JFrame();
    // Set it's size.                                                                                                                                                                                     
    frame.setSize(1000, 800);
    // Add the created helpViewer to it.                                                                                                                                                                  
    frame.getContentPane().add(helpViewer);
    // Set a default close operation.                                                                                                                                                                     
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setTitle(title);

    // Make the frame visible.                                                                                                                                                                            
    frame.setVisible(true);
}

} 我们可以根据需要自定义JFrame之后,很容易添加带有关闭按钮的南面板。

要使所有帮助按钮都收听我们的javahelp:

pButton.addActionListener(helpAction(pHelpId));

带有显示我们的JFrame的helpAction

还要考虑处理键盘快捷键,例如helpbroker:

pComponent.registerKeyboardAction(helpAction(pHelpId), KeyStroke.getKeyStroke(KeyEvent.VK_HELP, 0),
            JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    pComponent.registerKeyboardAction(helpAction(pHelpId), KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0),
            JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

要在帮助部分打开帮助,

helpViewer.setCurrentID("top");

“顶部”对应于.jhm文件中的标签