How do you get the screen width in java?

前端 未结 12 684
说谎
说谎 2020-12-13 08:52

Does anyone know how you would get the screen width in java? I read something about some toolkit method but I\'m not quite sure what that is.

Thanks, Andrew

12条回答
  •  情深已故
    2020-12-13 09:32

    Here are the two methods I use, which account for multiple monitors and task-bar insets. If you don't need the two methods separately, you can, of course, avoid getting the graphics config twice.

    static public Rectangle getScreenBounds(Window wnd) {
        Rectangle                           sb;
        Insets                              si=getScreenInsets(wnd);
    
        if(wnd==null) { 
            sb=GraphicsEnvironment
               .getLocalGraphicsEnvironment()
               .getDefaultScreenDevice()
               .getDefaultConfiguration()
               .getBounds(); 
            }
        else { 
            sb=wnd
               .getGraphicsConfiguration()
               .getBounds(); 
            }
    
        sb.x     +=si.left;
        sb.y     +=si.top;
        sb.width -=si.left+si.right;
        sb.height-=si.top+si.bottom;
        return sb;
        }
    
    static public Insets getScreenInsets(Window wnd) {
        Insets                              si;
    
        if(wnd==null) { 
            si=Toolkit.getDefaultToolkit().getScreenInsets(GraphicsEnvironment
               .getLocalGraphicsEnvironment()
               .getDefaultScreenDevice()
               .getDefaultConfiguration()); 
            }
        else { 
            si=wnd.getToolkit().getScreenInsets(wnd.getGraphicsConfiguration()); 
            }
        return si;
        }
    

提交回复
热议问题