我想将鼠标悬停在 GUI(地图)上的多个 JButton 上并显示该位置的名称,例如曼彻斯特和伦敦。我的代码适用于一个按钮,但它不适用于多个按钮并打印最后一个按钮out
所有按钮位置的消息(因为我有 10 个按钮)。
If button1
是 true 然后通过 my 在指定区域的 GUI 上绘制文本paintComponent()
method.
我该如何解决这个问题?
button1.addMouseMotionListener(this);
button2.addMouseMotionListener(this);
public void mouseMoved(MouseEvent arg0)
{
if(button1.contains(arg0.getPoint()))
{
button1 = true;
out = "test 1";
repaint();
}
if(!button1.contains(arg0.getPoint()))
{
b1 = false;
out = " ";
repaint();
}//same for all 10 buttons but change variables
}
为什么不使用已经存在的工具提示 API?
button.setTooltip("Manchester");
您甚至可以使用 HTML 文本来生成格式化结果。
button.setTooltip("<html>Manchester<br>53.4800° N, 2.2400° W</html>");
如果嵌入图像,您甚至可以提供图像......
button.setTooltip("<html><img src=" + getClass().getResource("/someimage") + "/>Manchester<br>53.4800° N, 2.2400° W</html>");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)