java - 每个带有问号光标的组件的上下文帮助

标签 java swing user-interface

大多数 Windows 用户可能记得每个 Windows 98 属性/设置窗口在其他窗口按钮旁边都有一个小问号按钮:

image description

如果您单击该按钮,所有单击事件都会被该窗口的不同回调覆盖。该新回调将显示元素的单独帮助文本。

我也想这样做。我的想法是使用包含所有 JComponentHelp 关联的类来实现:

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);
    }
  }
}

编写这两个类是比较容易的部分。困难的是:

  1. 如何覆盖特定窗口中的所有点击事件,然后在调用帮助后删除覆盖?
  2. 如何确保帮助光标覆盖所有其他光标,并再次安全地删除此设置?

我不知道从哪里开始。我真的不想仅仅因为这个而更改 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) {
     ???
  }
}

最佳答案

您可以尝试以下操作:

  1. 注册一个全局的 MouseListener 使用 Toolkit.getDefaultToolkit().addAWTEventListener(myListener, AWTEvent.MOUSE_EVENT_MASK)
  2. 将传入事件转换为 MouseEvent 并使用 getID() 方法检查事件类型
  3. 如果事件是对具有帮助的组件的点击,您需要显示帮助、使用事件并从全局监听器列表中删除此监听器。
  4. 你也可以覆盖 mouseEnter/Exit 事件 具有帮助文本的组件的监听器,并将光标设置为 问题/正常类型(不要忘记使用此事件)。

这只是你的想法,我没有测试过它是否有效。

关于java - 每个带有问号光标的组件的上下文帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29895526/

相关文章:

java - 如何验证 jtextfield 仅接受巴基斯坦 cnic 格式?

android:scrollHorizo​​ntally 不适用于 Samsung Galaxy Nexus

java - 堆大小错误 Aspose

java - 单个 while 循环的 Big-Oh 表示法,该循环覆盖具有两个迭代器变量的数组的两半

java - JComboBox 在 JTable TableHeader 中展开失败

ios - 如何制作 iOS 7 应用程序切换器中使用的滑动以终止动画?

Java Swing : GUI is not updating some properties

java - 为 apache httpcomponents 使用的线程命名

java - 关于数据库查询的简单问题

java - 如何使用 JProgressBar?