我的输出窗口如下所示我
的完整代码是:
http
://codes-at-igit.weebly.com/uploads/1/2/2/7/12272842/travellingsalesmanproblem.java
圆圈是不同的 GPS 位置。我想显示位置,即鼠标悬停在节点上时的经度和纬度。我尝试设置工具提示文本,但它没有指定文本应该出现的位置的特权。我已经用 swing Java 对其进行了编码。我在 Netbeans 7.1.2 中工作。那么我该怎么做呢?
如何在特定位置设置工具提示文本?
问问题
10105 次
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 回答