在特定位置设置工具提示文本 [英] set tooltip text at a particular location

查看:100
本文介绍了在特定位置设置工具提示文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的输出窗口如下所示:这里
我完整的代码是: http://codes-at-igit .weebly.com/uploads/1/2/2/7/12272842/travellingsalesmanproblem.java
圆圈是G.P.S的不同位置.我想显示鼠标悬停在节点上时的位置,即经度和纬度.我尝试设置工具提示文本,但是它没有赋予特权以指定文本应出现的位置.我已经用Swing Java编写了代码.我正在使用Netbeans 7.1.2.那我该怎么做呢? 如何在特定位置设置工具提示文本?

I have my output window as shown here
My complete code is: http://codes-at-igit.weebly.com/uploads/1/2/2/7/12272842/travellingsalesmanproblem.java
The circles are different G.P.S locations. I want to show the location i.e. , the longitude and latitude when mouse hovers on a node. I tried set tool tip text but it doesn't give privilege to specify the locations at which the text should occur. I have coded it in swing Java . I am working in Netbeans 7.1.2. So how can I do this? How do I set tool tip text at a particular position?

推荐答案

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

You can simply override public String getToolTipText(MouseEvent event) of the underlying JComponent. Then based on the location of the event you can return null or the tooltip related to the node.

这是一个演示此内容的小片段:

Here is a small snippet demonstrating this:

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();
            }
        });
    }

}

这篇关于在特定位置设置工具提示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆