How do you get the screen width in java?

前端 未结 12 528
说谎
说谎 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:30

    You can get it by using the AWT Toolkit.

    0 讨论(0)
  • 2020-12-13 09:30

    Toolkit.getScreenSize().

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    
    0 讨论(0)
  • 2020-12-13 09:31

    The working area is the desktop area of the display, excluding taskbars, docked windows, and docked tool bars.

    If what you want is the "working area" of the screen, use this:

    public static int GetScreenWorkingWidth() {
        return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width;
    }
    
    public static int GetScreenWorkingHeight() {
        return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height;
    }
    
    0 讨论(0)
  • 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;
        }
    
    0 讨论(0)
  • 2020-12-13 09:33

    If you need the resolution of the screen that a certain component is currently assigned to (something like most part of the root window is visible on that screen), you can use this answer.

    0 讨论(0)
  • 2020-12-13 09:35

    Toolkit has a number of classes that would help:

    1. getScreenSize - raw screen size
    2. getScreenInsets - gets size of toolbar, dock
    3. getScreenResolution - dpi

    We end up using 1 and 2, to compute usable maximum window size. To get the relevant GraphicsConfiguration, we use

    GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDefaultConfiguration();
    

    but there may be smarter multiple-monitor solutions.

    0 讨论(0)
提交回复
热议问题