如何弄清楚JDialog显示在哪个屏幕上 [英] How to figure out on which screen a JDialog is shown

查看:155
本文介绍了如何弄清楚JDialog显示在哪个屏幕上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a really big application which has multiple dialogs. My task is to make sure that a dialog, which is not completely visible (because the user pulled it out of the visible screen area) is moved back to the center of the screen.

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

That's no problem when I'm dealing with one screen only. It works just fine ... however, most users of this application have two screens on their desktop ...

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

When I try to figure out on which screen the dialog is shown and center it on that specific screen, ... well, it actually DOES center, but on the primary screen (which may not be the screen the dialog is shown on).

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

To show you what my thoughts were so far, here's the code ...

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

While debugging I kind of came across the fact that getActiveScreen() does not seem to work the way i though; it seems to always return 2 (which is kind of crap, since it would mean the dialog is always shown in the second monitor...which of course isn't the truth).

任何人都知道如何将对话框放在实际显示的屏幕上?

Anyone got any idea how to center my dialog on the screen it is actually shown on?

推荐答案

你的 getActiveScreen 方法有效,除了它使用包含窗口的左上角。如果您使用Component.getGraphicsConfiguration(),它将为您提供哪个屏幕具有窗口像素的大部分。 setLocationRelativeTo(null)在这里没有帮助,因为它总是使用主屏幕。以下是解决方法:

Your getActiveScreen method worked, except it used the screen containing the top-left corner of the window. If you use Component.getGraphicsConfiguration() instead, it will give you which screen has the most of the window's pixels. setLocationRelativeTo(null) is no help here because it always uses the primary screen. Here's how to solve it:

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);

将对话框置于最近的屏幕(显示器)中心。您可能需要确保最初显示/定位对话框。

which will center the dialog to the nearest screen (monitor). You might need to make sure the dialog has been initially displayed/positioned first.

这篇关于如何弄清楚JDialog显示在哪个屏幕上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆