Ste*_*han 3 java swing multiple-monitors popupmenu
我想设置JPopupMenu打开菜单的按钮的y位置的位置.我的代码在我的第一台显示器上工作正常,但在我的第二台显示器上失败,它有不同的高度.问题是getLocationOnScreen()提供相对于主屏幕的位置,而不是显示组件的实际屏幕.
我的代码:
// screenSize represents the size of the screen where the button is
// currently showing
final Rectangle screenSize = dateButton.getGraphicsConfiguration().getBounds();
final int yScreen = screenSize.height;
int preferredY;
// getLocationOnScreen does always give the relative position to the main screen
if (getLocationOnScreen().y + dateButton.getHeight() + datePopup.getPreferredSize().height > yScreen) {
preferredY = -datePopup.getPreferredSize().height;
} else {
preferredY = getPreferredSize().height;
}
datePopup.show(DateSpinner.this, 0, preferredY);
Run Code Online (Sandbox Code Playgroud)
如何在实际监视器上获取组件的位置?
我使用第二个屏幕的边界得到了一个解决方案,这很简单:
public static Point getLocationOnCurrentScreen(final Component c) {
final Point relativeLocation = c.getLocationOnScreen();
final Rectangle currentScreenBounds = c.getGraphicsConfiguration().getBounds();
relativeLocation.x -= currentScreenBounds.x;
relativeLocation.y -= currentScreenBounds.y;
return relativeLocation;
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的回答!
| 归档时间: |
|
| 查看次数: |
3644 次 |
| 最近记录: |