我有一个JPopupMenu对象。它的行为取决于它的坐标。我怎样才能得到它相对于它的父容器的位置?
在你的MouseListener
的方法(mouserrelease等),你应该收到一个MouseEvent
对象包含当前的位置。如果您不想使用这些值,您可以尝试使用Component#getLocation
方法,否则Component#getLocationOnScreen
但它返回绝对位置,然后您需要计算相对位置。
有一个解决方案,但不推荐,因为如果有SecurityManager,它可能会失败(强制该字段可访问):
public static Container getTopParent(@Nonnull Component c) {
Container lastNotNull = (Container) c;
Container p = c.getParent();
if (p != null)
lastNotNull = p;
while(p != null) {
lastNotNull = p;
p = p.getParent();
}
return lastNotNull;
}
public static int getClickedXThatInvokedPopup(@Nonnull ActionEvent ev) {
try {
JPopupMenu topParent = (JPopupMenu) getTopParent((Component) ev.getSource());
java.lang.reflect.Field fieldX = topParent.getClass().getDeclaredField("desiredLocationX");
fieldX.setAccessible(true);
int x = (Integer) fieldX.get(topParent);
Point p = new Point(x, 0);
SwingUtilities.convertPointFromScreen(p, topParent.getInvoker());
return p.x;
} catch(NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) {
System.err.println("Cannot get clicked point: " + ex);
return -1;
}
}
由于Component#getLocation
和Component#getLocationOnScreen
方法都不适合我,并且desiredLocationX
/desiredLocationY
字段不可访问,我扩展了JPopupMenu()如下:
contextMenu = new JPopupMenu(){
private Point desiredLocation;
/**
* Override Component#getLocation, since it always returns 0,0.
*/
@Override
public Point getLocation() {
return desiredLocation;
}
@Override
public void show(Component invoker, int x, int y) {
desiredLocation = new Point(x, y);
super.show(invoker, x, y);
}
};