当我的 button2 获得焦点时,我需要以编程方式显示它的工具提示。(我按 Tab 作为最初的焦点在按钮 1)
JButton button = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
button2.setToolTipText("tooltip2");
button2.addFocusListener(new FocusListener());
我参考@camickr的代码
private class FocusListener extends FocusAdapter {
public void focusGained(FocusEvent e)
{
JComponent component = (JComponent)e.getSource();
Action toolTipAction = component.getActionMap().get("postTip");
但 toolTipAction 设置为空。
我已经使用此代码打印了 ActionMap 的所有条目
ActionMap actionMap = component.getActionMap();
Object[] actionMapKeys = actionMap.allKeys();
for (int i = 0; i < actionMapKeys.length; i++) {
Object key = actionMapKeys[i];
System.out.println(key.toString() + " : " + actionMap.get(key).toString());
}
这就是它给我的
pressed : javax.swing.plaf.basic.BasicButtonListener$Actions@49cf9f
released : javax.swing.plaf.basic.BasicButtonListener$Actions@1de0b5e
那么如果我的 toolTipAction 为空,我该如何调用这段代码呢?
ActionEvent postTip = new ActionEvent(component, ActionEvent.ACTION_PERFORMED, "");
toolTipAction.actionPerformed(postTip);