是否可以使显示的工具提示在某个 Swing 对象内的某些关键字上发生变化?内容将完全由代码定义,因此如果有帮助的话我可以使用文本定位。谢谢
编辑:
好吧,具体来说,我要做的是创建一个自定义显示框,用于将文本用作战斗日志。如果你愿意的话,这将超越我的动画,一个平视显示器。我的想法是,我可以使用工具提示来显示战斗计算背后的属性,同时为那些已经知道事情如何运作的人整理日志。
最佳答案
重写getToolTipText(...)
。简单的例子:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.UIManager.*;
public class ToolTipPanel extends JPanel
{
public ToolTipPanel()
{
setPreferredSize( new Dimension(200, 200) );
setToolTipText("");
}
public void paintComponent(Graphics g)
{
g.setColor( Color.red );
g.fillRect(0, 0, 100, 200);
g.setColor( Color.blue );
g.fillRect(100, 0, 100, 200);
}
public String getToolTipText(MouseEvent e)
{
if (e.getX() < 100)
return "red";
else
return "blue";
}
public Point getToolTipLocation(MouseEvent e)
{
Point p = e.getPoint();
p.y += 15;
return p;
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.getContentPane().add( new ToolTipPanel() );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}
对于文本组件,您需要使用鼠标点和 getViewToModel() 方法从文档中获取文本。
关于java - 工具提示文本根据鼠标悬停在文本框中的单词而变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17430095/