我想設置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);
我怎樣才能實際顯示器上得到一個組件的位置?
如果輔助監視器是相反的站點,該怎麼辦?如果主顯示器和輔助顯示器發生變化怎麼辦? – Stefan
不應該是「+ =」? –