gpt4 book ai didi

java - JButton 以编程方式显示工具提示 : actionMap. get ("postTip") 为空

转载 作者:行者123 更新时间:2023-11-29 03:40:01 24 4
gpt4 key购买 nike

我需要在 button2 获得焦点时以编程方式显示工具提示。 (我按 Tab 键是因为 initially focus at button1)

            JButton button = new JButton("Button 1");

JButton button2 = new JButton("Button 2");
button2.setToolTipText("tooltip2");
button2.addFocusListener(new FocusListener());

我指的是 code通过@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 为 null,我该如何调用这段代码呢?

ActionEvent postTip = new ActionEvent(component, ActionEvent.ACTION_PERFORMED, "");
toolTipAction.actionPerformed(postTip);

最佳答案

actuall I want to show tooltip on some network event, which has nothing to do with mouse. but I have started experiments with focus gained by Tab key

关于java - JButton 以编程方式显示工具提示 : actionMap. get ("postTip") 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13599079/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com