java - 如何确定在哪个屏幕上显示 JDialog

标签 java swing center jdialog multiple-monitors

我有一个非常大的应用程序,它有多个对话框。我的任务是确保将不完全可见的对话框(因为用户将其拉出可见屏幕区域)移回屏幕中央。

当我只处理一个屏幕时,这没问题。 它工作得很好......但是,这个应用程序的大多数用户在他们的桌面上有两个屏幕......

当我试图找出对话框显示在哪个屏幕上并将其居中放置在该特定屏幕上时,......好吧,它确实居中,但在主屏幕上(可能不是显示对话框的屏幕)在)。

为了向您展示我到目前为止的想法,这是代码......

 /**
 * 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(这有点废话,因为这意味着对话框总是显示在第二个监视器中……这当然不是事实)。

有人知道如何将我的对话框置于实际显示的屏幕上吗?

最佳答案

您的 getActiveScreen 方法有效,只是它使用了包含窗口左上角的屏幕。如果您改用 Component.getGraphicsConfiguration(),它会告诉您哪个屏幕具有最多的窗口像素。 setLocationRelativeTo(null) 在这里没有帮助,因为它始终使用主屏幕。解决方法如下:

static boolean windowFitsOnScreen(Window w) {
    return w.getGraphicsConfiguration().getBounds().contains(w.getBounds());
}

static void centerWindowToScreen(Window w) {
    Rectangle screen = w.getGraphicsConfiguration().getBounds();
    w.setLocation(
        screen.x + (screen.width - w.getWidth()) / 2,
        screen.y + (screen.height - w.getHeight()) / 2
    );
}

然后你可以这样做:

JDialog jd;
...
if (!windowFitsOnScreen(jd)) centerWindowToScreen(jd);

这将使对话框居中到最近的屏幕(显示器)。您可能需要先确保对话框已初始显示/定位。

关于java - 如何确定在哪个屏幕上显示 JDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12158548/

相关文章:

java - 拖动后执行意外操作

java - 什么是 SwingUtilities.invokeLater

css - 强制居中对齐内容

java - 在 Java 中使用 MVC 模式

Java:Math.pow 返回令人困惑的结果

java - 为什么有些类限制直接实例化?

html - 列数为奇数的表中的偶数个 td 元素

java - 将 Spring Socials (Facebook) 与基于 XML 的 Spring MVC 集成

java - 将 xuggler 包裹在 JPanel 中

html - 我需要将图像放在画廊的中心,但没有任何效果