5

ここに示すような出力ウィンドウがあります 。
完全なコードは次 のとおりです。 http://codes-at-igit.weebly.com/uploads/1/2/2/7/12272842/travellingsalesmanproblem.java
マウスがノード上に置かれたときの場所、つまり経度と緯度を表示したい。ツール ヒント テキストを設定しようとしましたが、テキストが表示される場所を指定する権限がありません。私はそれをスイング Java でコーディングしました。私は Netbeans 7.1.2 で作業しています。では、どうすればこれを行うことができますか? 特定の位置にツール ヒント テキストを設定するにはどうすればよいですか?

4

2 に答える 2

6

public String getToolTipText(MouseEvent event)基礎となる JComponentを単純にオーバーライドできます。次に、イベントの場所に基づいて、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();
            }
        });
    }

}
于 2012-07-07T13:50:12.360 に答える
4

たとえば、getToolTipLocation() をオーバーライドしてみてください。

JButton buttonAbove = new JButton("Above") {
      public Point getToolTipLocation(MouseEvent e) {
        return new Point(20, -30);
      }
    };

ここから: http://www.java2s.com/Code/Java/Swing-JFC/ToolTipLocationExample.htm

于 2012-12-30T13:51:22.427 に答える