Java 将鼠标悬停在JButtons上并显示消息
我想将鼠标悬停在GUI(地图)上的多个JButton上,并显示该位置的名称,例如曼彻斯特和伦敦。我的代码适用于一个按钮,但它不适用于多个按钮,并为所有按钮位置打印最后一条Java 将鼠标悬停在JButtons上并显示消息,java,swing,tooltip,jbutton,mousemotionevent,Java,Swing,Tooltip,Jbutton,Mousemotionevent,我想将鼠标悬停在GUI(地图)上的多个JButton上,并显示该位置的名称,例如曼彻斯特和伦敦。我的代码适用于一个按钮,但它不适用于多个按钮,并为所有按钮位置打印最后一条信息(因为我有10个按钮) 如果button1为true,则它将通过我的paintComponent()方法在GUI上的指定区域绘制文本 我如何解决这个问题 button1.addMouseMotionListener(this); button2.addMouseMotionListener(this); 不要使用JBut
信息(因为我有10个按钮)
如果button1
为true,则它将通过我的paintComponent()
方法在GUI上的指定区域绘制文本
我如何解决这个问题
button1.addMouseMotionListener(this);
button2.addMouseMotionListener(this);
- 不要使用
JButton
中的MouseListener
或MosueMotionListener
,此方法在JButtons API
中正确实现
- 没有理由,我找不到对此作业使用
repaint()
的理由
- 另一种方法是将
ChangeListener
添加到JButton
并从派生的ButtonModel
- 为了获得更好的帮助,请尽快发布一个简短的、可运行的、可编译的、大约
JFrame
和一个JButton
为什么不使用已经存在的工具提示API
button.setTooltip("Manchester");
您甚至可以使用HTML文本生成格式化的结果
button.setTooltip("<html>Manchester<br>53.4800° N, 2.2400° W</html>");
按钮。设置工具提示(“曼彻斯特
53.4800°N,2.2400°W”);
如果图像是嵌入的,你甚至可以提供一个图像
button.setTooltip("<html><img src=" + getClass().getResource("/someimage") + "/>Manchester<br>53.4800° N, 2.2400° W</html>");
按钮。设置工具提示(“曼彻斯特
53.4800°N,2.2400°W”);
这个答案是针对JDK 8用户的coolio,所以请尝试一下:
对于常规文本
buttonyoumade.setToolTipText("Text you choose");
供html使用
anotherbuttonyoumade.setToolTipText("<html> any valid html code </html>");
anotherbuttonyoumade.setToolTipText(“任何有效的html代码”);
“如果图像被嵌入,你甚至可以提供一个图像…”它们不一定要被嵌入,但如果是这样,a)更快&b)可被沙盒应用程序使用。@andrewhompson啊,这可能是任何有效的URL
anotherbuttonyoumade.setToolTipText("<html> any valid html code </html>");