首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向javahelp添加按钮菜单

向javahelp添加按钮菜单
EN

Stack Overflow用户
提问于 2018-09-27 13:23:07
回答 1查看 104关注 0票数 0

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

我希望自定义的helpbroker在窗口底部添加一个按钮条,就像在图像中一样。

做这件事最简单的方法是什么?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-10 12:45:12

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

代码语言:javascript
复制
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:

代码语言:javascript
复制
pButton.addActionListener(helpAction(pHelpId));

使用helpAction显示我们的JFrame

还可以考虑处理键盘快捷键,比如helpbroker:

代码语言:javascript
复制
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);

若要在通缉部分打开帮助:

代码语言:javascript
复制
helpViewer.setCurrentID("top");

"top“对应于.jhm文件中的标记

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52538066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档