我有一个非常大的应用程序,它有多个对话框.我的任务是确保一个不完全可见的对话框(因为用户将其从可见屏幕区域拉出)被移回屏幕中心.
当我只处理一个屏幕时,这没问题.它工作得很好......但是,这个应用程序的大多数用户在他们的桌面上有两个屏幕...
当我试图弄清楚对话框显示在哪个屏幕上并将其置于该特定屏幕上时,......好吧,它实际上是中心,但是在主屏幕上(可能不是屏幕上显示对话框).
为了向您展示我到目前为止的想法,这里的代码是......
/**
* Get the number of the screen the dialog is shown on ...
*/
private static int getActiveScreen(JDialog jd) {
int screenId = 1;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gd = ge.getScreenDevices();
for (int i = 0; i < gd.length; i++) {
GraphicsConfiguration gc = gd[i].getDefaultConfiguration();
Rectangle r = gc.getBounds();
if (r.contains(jd.getLocation())) {
screenId = i + 1;
}
}
return screenId;
}
/**
* Get the Dimension of the screen with the given id …Run Code Online (Sandbox Code Playgroud)