2

不介意Window Insets的使用,但更注意ScreenInsets的使用,它在本地保存为Insets insets;我打印 insets.bottom,并且对于每个监视器,任务栏高度都会显示,即使任务栏仅位于第一台监视器上。

我的第二台显示器上的显示器插图都应该为零,但它的作用就像任务栏位于两个显示器上一样。在窗口当前所在的监视器中将窗口设置为全尺寸,除了它为任务栏留出空间,无论在哪个监视器中使用它。

根据我对 Toolkit.getScreenInsets(GraphicsConfiguration) 的理解,它应该为您传入的特定 GraphicsConfiguration 返回正确的插图,但我传入每个 GraphicsDevice 的 GraphicsConfiguration 并得到相同的结果。

JFrame window;

public void setSizeToFullScreen()
    {
        GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment(); 
        GraphicsDevice[] screenDevices=ge.getScreenDevices();
        Point p=window.getLocationOnScreen();       
            for(int i=0;i<screenDevices.length;i++)
            {

                Rectangle2D b=screenDevices[i].getDefaultConfiguration().getBounds();
                if(SMath.getMath().doesRectangleContainPoint(b.getX(), b.getY(), b.getWidth(), b.getHeight(), p.getX(),p.getY()))
                {
                    Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(screenDevices[i].getDefaultConfiguration());
                    System.out.println("Monitor:  "+i+":  task bar height:  "+insets.bottom);
                    this.setSize(b.getWidth()+1 -(insets.right+insets.left)-(this.window.getInsets().left+this.window.getInsets().right), b.getHeight()+1-(insets.top+insets.bottom)-(this.window.getInsets().top+this.window.getInsets().bottom));
                    this.setLocation(b.getX()+insets.left+window.getInsets().left, b.getY()+insets.top+window.getInsets().top);
                    return;
                }
            }       
    }

我的问题是,在 Java 中,我们如何才能确定哪个监视器实际上有任务栏,或者更好的问题是,我们如何才能在 Java 中为每个监视器获取正确的监视器插图。

4

1 回答 1

0

回复:“...,但它的作用就像任务栏位于两个显示器上一样”。

我发现了以下内容:

参考Windows->控制面板->外观和个性化->显示->屏幕分辨率:

当 gs[0](= 在上面的控制面板窗口中的圆圈内显示“1”)具有工具栏时,报告的插入是正确的。

即,对于没有工具栏的屏幕,它们被报告为 = 0,对于有工具栏的屏幕,它们被报告为 = 49。

当任何其他 gs[x] 具有工具栏时,报告的插图是错误的 :-(。即,对于所有屏幕,它们报告为 = 49。

在我的应用程序中,我希望“JDialog dialog0”始终出现在“大”显示的左下角右侧 500 处,并且“JFrame frameBalloonHerderGui”始终出现在“小”显示的左上角.

我希望 JDialog 在我的“大”显示屏的左下角具有固定大小,并且 JFrame 应该几乎填满它所在的“小”显示屏。

我确实为我希望每个 JDialog/JFrame 出现在它们的构造函数中的显示提供了 graphicsConfiguration。唉,这还不足以让插图是正确的。

为了完成上面的定位,我编写了一个函数来调用我的 JDialog 和 JFrame,如下所示:

// I'll omit the creation of the grapicsConfiguration for now.
JDialog dialog0 = new JDialog( gc);  

ScreenAndTaskBarHeights h = getThisComponentsScreensTaskBarHeight( dialog0);

final int myWidth = 1170;
final int myHeight = 800;
final int myScreenXInset = 500;
final int myScreenYInset = 10;

dialog0.setBounds( 
    h.screenOriginX + myScreenXInset, 
    h.screenOriginY + h.screenHeight - myHeight - h.taskBarHeight - myScreenYInset,
    myWidth, myHeight);

dialog0.setVisible( true);

// I'll omit the creation of the grapicsConfiguration for now.
JFrame frameBalloonHerderGui = new JFrame( gc);

ScreenAndTaskBarHeights h = 
    getThisComponentsScreensTaskBarHeight( frameBalloonHerderGui);

final int myWidth = 1695;
final int myScreenInset = 10;

frameBalloonHerderGui.setBounds( 
    h.screenOriginX + myScreenInset,  
    h.screenOriginY + myScreenInset,
    myWidth, h.screenHeight - (myScreenInset * 2) - h.taskBarHeight);

frameBalloonHerderGui.setVisible( true);

我发现的解决方法是 screenSize.x & .y = (0,0) 用于具有任务栏的显示器,以及一些大的正数或负数用于其他显示器。

下面的函数成功地实现了这个变通方法。我还创建了一个简单的类,因此我可以将多个值传递回调用者,如上所示。

// Add additional data members here to your liking.
static class ScreenAndTaskBarHeights
{
    int screenOriginX = -1;
    int screenOriginY = -1;
    int screenHeight  = -1;
    int taskBarHeight = -1;

    ScreenAndTaskBarHeights()
    {           
    }

    void setValues(
        int newScreenOriginX, int newScreenOriginY,
        int newScreenHeight, int newTaskBarHeight)
    {
        screenOriginX = newScreenOriginX;
        screenOriginY = newScreenOriginY;
        screenHeight  = newScreenHeight;
        taskBarHeight = newTaskBarHeight;
    }
}

static ScreenAndTaskBarHeights 
getThisComponentsScreensTaskBarHeight( Component c)
{
    ScreenAndTaskBarHeights screenAndTaskBarHeights =
        new ScreenAndTaskBarHeights();

    GraphicsConfiguration gc = c.getGraphicsConfiguration();

    Insets scnMax = Toolkit.getDefaultToolkit().getScreenInsets( gc);

    // This should be the TaskBar height specific to the gc that we 
    // passed in, but it's not :-(.
    //
    //      int taskBarHeight = scnMax.bottom;
    //
    // However, this seems to be a successful workaround:
    //
    Rectangle screenSize = gc.getBounds();

    boolean thisScreenHasTheToolbar =
        (screenSize.x == 0 && screenSize.y == 0);

    // Change scnMax.bottom to catch wherever you're worried that the
    // TaskBar may be lurking.
    //
    screenAndTaskBarHeights.setValues(
        screenSize.x, screenSize.y, screenSize.height,
        (thisScreenHasTheToolbar) ? scnMax.bottom : 0);

    return screenAndTaskBarHeights;     
}
于 2017-01-20T01:33:54.727 回答