提问人:TruthIZ 提问时间:3/19/2021 更新时间:3/19/2021 访问量:228
如何在鼠标移动创建的矩形上使用工具提示 - Java
How to use tooltips on rectangle created by mouse movement - Java
问:
我正在用鼠标创建一个矩形,我只是想用鼠标悬停从创建的对象中返回信息。我一直在用messabox解决这个问题,但是没有多少国家可以悬停,所以弹出窗口的数量可能非常多。
我决定使用工具提示。问题是它不能在我的鼠标上工作:
JTextField text = new JTextField();
if (coordX > coordXRec && coordX < coordXRec + width && coordY > coordYrec && coordY < coordYrec + height){
text.setToolTipText(i.GetInfoPays());
text.getToolTipText();
}
当然,我希望它在我将鼠标悬停在创建的矩形上时显示:
那个绿色矩形是由用户制作的,所以我无法“预设”事件 或者将其用作我的工具提示的面板。
我一直在使用 MouseEventMoved 来了解我是否将鼠标悬停在我的矩形上。它有效,但我被困在将我的消息框更改为工具提示。
答:
5赞
Andrew Thompson
3/19/2021
#1
在需要时(例如在 期间)与适当的一起使用。setToolTipText("...");
String
paintComponent()
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.*;
public class MouseShapeDetection {
private JComponent ui = null;
MouseShapeDetection() {
initUI();
}
public final void initUI() {
if (ui != null) {
return;
}
ui = new JPanel(new BorderLayout(4, 4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));
ui.add(new ShapePanel());
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = () -> {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
MouseShapeDetection o = new MouseShapeDetection();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
};
SwingUtilities.invokeLater(r);
}
}
class ShapePanel extends JPanel {
Point point = new Point(0, 0);
Dimension preferredSize = new Dimension(600, 300);
ArrayList<Shape> shapes = new ArrayList<>();
Color translucent = new Color(0,0,255,87);
Color selectedColor = Color.GREEN;
Color unselectedColor = Color.RED;
public ShapePanel() {
this.addMouseMotionListener(new MotionListener());
setBackground(Color.WHITE);
Random r = new Random();
int x, y, w, h, wP = preferredSize.width, hP = preferredSize.height;
for (int ii = 0; ii < 40; ii++) {
w = r.nextInt(100)+40;
h = r.nextInt(50)+20;
x = r.nextInt(wP - w);
y = r.nextInt(hP - h);
Ellipse2D.Double ellipse = new Ellipse2D.Double(x, y, w, h);
shapes.add(ellipse);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
StringBuilder sb = new StringBuilder("Shapes: ");
Graphics2D g2 = (Graphics2D)g;
for (int ii=0; ii<shapes.size(); ii++) {
Shape shape = shapes.get(ii);
g2.setColor(translucent);
g2.fill(shape);
if (shape.contains(point)) {
g2.setColor(selectedColor);
sb.append(ii);
sb.append(" ");
} else {
g2.setColor(unselectedColor);
}
g2.setStroke(new BasicStroke(2.5f));
g2.draw(shape);
}
setToolTipText(sb.toString());
}
@Override
public Dimension getPreferredSize() {
return preferredSize;
}
class MotionListener extends MouseMotionAdapter {
@Override
public void mouseMoved(MouseEvent e) {
point = e.getPoint();
repaint();
}
}
}
评论
0赞
Andrew Thompson
1/26/2022
如果这个答案有帮助,请点击它左侧的“勾号”接受它。如果没有,解释会有所帮助。答案被接受的问题会在搜索结果中优先排序,使用户能够更轻松地找到解决方案。
评论