覆盖
getToolTipText(MouseEvent)根据鼠标位置动态设置工具提示的方法。
编辑:
如果您希望工具提示不断用鼠标移动,则还需要重写该
getToolTipLocation()方法。
import java.awt.*;import java.awt.event.*;import javax.swing.*;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;// return super.getToolTipLocation(e); } 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); }}


