大多数Windows用户可能还记得每个Windows 98属性/设置窗口旁边都有一个小问号按钮:
如果单击该按钮,则所有单击事件都会被该窗口的不同回调覆盖。而新的回调将显示元素的个人帮助文本。
我想做同样的事情。我的想法是使用包含所有JComponent
和Help
关联的类来完成它:
public interface Help {
/** based on implementation, displays help to the used. May use
* JDialog, url redirection or maybe open document on the computer.**/
public void getHelp(JComponent comp, ActionEvent evt);
}
public class HelpLibrary {
public HashMap<JComponent, Help> helpLib;
public void getHelp(JComponent comp, ActionEvent evt) {
Help help = helpLib.get(comp);
if(help!=null) {
help.getHelp(comp, evt);
}
}
}
写这两个课程很容易。困难的是:
我不知道从哪里开始。我真的不想因为这个而改变GUI结构或使用类,这就是我想存储帮助并从外部进行覆盖的原因。
public class HelpLibrary {
/**
* Overrides click events on the given window and displays help cursor.
* User then may click a JComponent, such as button, to initiate
* help callback for that element. If no help exists for that element,
* do nothing and stop the help mode.
* @param window the window to get help for
**/
public void waitForHelp(JFrame window) {
???
}
}
答案 0 :(得分:1)
您可以尝试以下操作:
Toolkit.getDefaultToolkit().addAWTEventListener(myListener, AWTEvent.MOUSE_EVENT_MASK)
这只是你的想法,我没有测试它是否有效。
答案 1 :(得分:0)
您可以使用GlassPane
。
阅读How to Use Root Panes上的Swing教程中的部分。 Glass Pane演示展示了如何拦截鼠标事件并将事件重新发送到底层组件。 YoOu显然会更改此代码以在鼠标单击下找到该组件,然后显示帮助上下文。
可以通过使玻璃窗可见或不可以打开/关闭玻璃窗。