我的输出窗口显示为here
我的完整代码是:
http://codes-at-igit.weebly.com/uploads/1/2/2/7/12272842/travellingsalesmanproblem.java
圆圈是不同的G.P.S位置。我想显示鼠标悬停在节点上的位置,即经度和纬度。我尝试设置工具提示文本但它没有权限指定文本应该出现的位置。我用swing Java编写了它。我在Netbeans 7.1.2工作。那我该怎么做呢?
如何在特定位置设置工具提示文字?
答案 0 :(得分:6)
您可以简单地覆盖基础JComponent的public String getToolTipText(MouseEvent event)
。然后根据事件的位置,您可以返回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();
}
});
}
}
答案 1 :(得分:2)
尝试覆盖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