gpt4 book ai didi

java - setToolTipText 消耗鼠标事件的解决方法?

转载 作者:搜寻专家 更新时间:2023-10-31 20:07:10 25 4
gpt4 key购买 nike

这似乎是一个经过验证的 SWING 问题

http://forums.sun.com/thread.jspa?threadID=385730

我目前正在尝试在 JTabbedPane 中设置选项卡的工具提示文本,但是当我这样做时,我实际上无法再选择选项卡,因为工具提示添加了一个正在消耗事件。

有谁知道可以让我保留工具提示和鼠标事件的解决方法?谢谢。

根据要求,这是我的 SSCCE

package jtabbedbug;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;

public class JTabBug{

public static void main(String[] args) {

JTabbedPane jTabbedPane = new JTabbedPane();
jTabbedPane.addTab("Red", new JLabel("Roses"));
jTabbedPane.addTab("Blue", new JLabel("Skies"));
jTabbedPane.addTab("Green", new JLabel("Grass"));

for (int i = 0; i < jTabbedPane.getTabCount(); i++) {
JLabel tabComponent = new JLabel(jTabbedPane.getTitleAt(i));
tabComponent.setToolTipText("Tip: " + tabComponent.getText());
jTabbedPane.setTabComponentAt(i, tabComponent);
}

JFrame jFrame = new JFrame("Testing");
jFrame.add(jTabbedPane);
jFrame.setSize(400, 500);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

最佳答案

这是一种解决方法:

import javax.swing.*;
import javax.swing.plaf.*;
import java.awt.event.*;

public class JTabBug{

public static void main(String[] args) {

JTabbedPane jTabbedPane = new JTabbedPane()
{
@Override
public String getToolTipText(MouseEvent e)
{
int index = ((TabbedPaneUI)ui).tabForCoordinate(this, e.getX(), e.getY());

if (index != -1)
{
JComponent component = (JComponent)getTabComponentAt(index);
return component.getToolTipText();
}

return super.getToolTipText(e);
}
};
ToolTipManager.sharedInstance().registerComponent(jTabbedPane);
jTabbedPane.addTab("Red", new JLabel("Roses"));
jTabbedPane.addTab("Blue", new JLabel("Skies"));
jTabbedPane.addTab("Green", new JLabel("Grass"));

for (int i = 0; i < jTabbedPane.getTabCount(); i++) {
JLabel tabComponent = new JLabel(jTabbedPane.getTitleAt(i));
tabComponent.setToolTipText("Tip: " + tabComponent.getText());
ToolTipManager.sharedInstance().unregisterComponent(tabComponent);
jTabbedPane.setTabComponentAt(i, tabComponent);
}

JFrame jFrame = new JFrame("Testing");
jFrame.add(jTabbedPane);
jFrame.setSize(400, 500);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

关于java - setToolTipText 消耗鼠标事件的解决方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3198037/

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