我有一個非常大的應用程序,它有多個對話框。我的任務是確保一個不完全可見的對話框(因爲用戶將它拉出可視屏幕區域)被移回到屏幕中心。如何找出顯示JDialog的屏幕
當我只處理一個屏幕時,這沒有問題。 它工作得很好...但是,這個應用程序的大多數用戶有兩個屏幕在他們的桌面上...
當我試圖找出在哪個屏幕上顯示對話框並將其居中在該特定屏幕上。 ..嗯,它實際上居中,但在主屏幕上(可能不是顯示對話框的屏幕)。
爲了告訴你什麼是我的想法是,到目前爲止,這裏的代碼...
/**
* 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 ...
*/
private static Dimension getScreenDimension(int screenId) {
Dimension d = new Dimension(0, 0);
if (screenId > 0) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
DisplayMode mode = ge.getScreenDevices()[screenId - 1].getDisplayMode();
d.setSize(mode.getWidth(), mode.getHeight());
}
return d;
}
/**
* Check, if Dialog can be displayed completely ...
* @return true, if dialog can be displayed completely
*/
private boolean pruefeDialogImSichtbarenBereich() {
int screenId = getActiveScreen(this);
Dimension dimOfScreen = getScreenDimension(screenId);
int xPos = this.getX();
int yPos = this.getY();
Dimension dimOfDialog = this.getSize();
if (xPos + dimOfDialog.getWidth() > dimOfScreen.getWidth() || yPos + dimOfDialog.getHeight() > dimOfScreen.getHeight()) {
return false;
}
return true;
}
/**
* Center Dialog...
*/
private void zentriereDialogAufMonitor() {
this.setLocationRelativeTo(null);
}
在調試我有種跨越的事實,getActiveScreen()
似乎並沒有在上班的路上我雖然來到;它似乎總是返回2(這是一種廢話,因爲這意味着對話總是顯示在第二個顯示器中......這當然不是事實)。
任何人都知道如何將我的對話框居中顯示在屏幕上?
應用程序會在多個顯示器上顯示多少個屏幕? –
我不太清楚如果我得到你的問題,但如果我理解正確,......我不知道。它甚至都不重要,因爲每當對話變得可見時,對話框的位置被保存並檢查......這是否回答你的問題? :-) – gilaras
爲什麼不在屏幕上指定對話框的位置? –