【发布时间】:2023-03-14 13:59:01
【问题描述】:
我想为这个多边形添加一个慕斯监听器。如何将鼠标监听器添加到图形Polygon?
public class Domx extends JPanel{
Domx(){
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
this.setBackground(Color.white);
Polygon p4=new Polygon();
for (int i = -1; i < 2; i++) {
p4.addPoint((int) (X + i * P4[9] / 2), (int) (Y + P1[10] + P2[10] + P3[10] + P4[10]));
}
for (int i = 1; i > -2; i--) {
p4.addPoint((int) (X + i * P4[8] / 2), (int) (Y + P1[10] + P2[10] + P3[10]));
}
g2.fillPolygon(p4);
}
}
【问题讨论】:
-
你想用鼠标监听器实现什么?
-
我想为这个多边形添加一个mousse监听器。 - 你不能直接这样做。您只能将 MouseListener 添加到组件。见安德鲁的回答。如果您想获得花哨的效果,请查看 Playing With Shapes 的
ShapeComponent类,它允许您使用多边形的形状创建组件。