java提示文本_java – 在特定位置设置工具提示文本

您可以简单地覆盖底层JComponent的公共String getToolTipText(MouseEvent事件).然后根据事件的位置,您可以返回null或与节点相关的工具提示.

这是一个小片段,展示了这一点:

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Point;

import java.awt.event.MouseEvent;

import java.awt.geom.Ellipse2D;

import java.beans.Transient;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.SwingUtilities;

import javax.swing.ToolTipManager;

public class TestTooltip {

private static class CirclePanel extends JPanel {

private Ellipse2D circle1 = new Ellipse2D.Double(0, 0, 20, 20);

private Ellipse2D circle2 = new Ellipse2D.Double(300, 200, 20, 20);

private Ellipse2D circle3 = new Ellipse2D.Double(200, 100, 20, 20);

public CirclePanel() {

// Register the component on the tooltip manager

// So that #getToolTipText(MouseEvent) gets invoked when the mouse

// hovers the component

ToolTipManager.sharedInstance().registerComponent(this);

}

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

// Simple paint of 3 circles on the component

g.setColor(Color.RED);

Graphics2D g2 = (Graphics2D) g;

g2.fill(circle1);

g2.fill(circle2);

g2.fill(circle3);

};

/**

* This method is called automatically when the mouse is over the component.

* Based on the location of the event, we detect if we are over one of

* the circles. If so, we display some information relative to that circle

* If the mouse is not over any circle we return the tooltip of the

* component.

*/

@Override

public String getToolTipText(MouseEvent event) {

Point p = new Point(event.getX(), event.getY());

String t = tooltipForCircle(p, circle1);

if (t != null) {

return t;

}

t = tooltipForCircle(p, circle2);

if (t != null) {

return t;

}

t = tooltipForCircle(p, circle3);

if (t != null) {

return t;

}

return super.getToolTipText(event);

}

@Override

@Transient

public Dimension getPreferredSize() {

// Some size we would like to have

return new Dimension(350, 350);

}

protected String tooltipForCircle(Point p, Ellipse2D circle) {

// Test to check if the point is inside circle

if (circle.contains(p)) {

// p is inside the circle, we return some information

// relative to that circle.

return "Circle: (" + circle.getX() + " " + circle.getY() + ")";

}

return null;

}

}

protected void initUI() {

JFrame frame = new JFrame("Test tooltip");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new CirclePanel();

frame.add(panel);

frame.pack();

frame.setVisible(true);

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

new TestTooltip().initUI();

}

});

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值