How do you get the screen width in java?

前端 未结 12 527
说谎
说谎 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:22
    java.awt.Toolkit.getDefaultToolkit().getScreenSize()
    
    0 讨论(0)
  • 2020-12-13 09:24

    The OP probably wanted something like this:

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

    This is an improvement to the multi-monitor solution posted (above) by Lawrence Dol. As in his solution, this code accounts for multiple monitors and task-bar insets. The included functions are: getScreenInsets(), getScreenWorkingArea(), and getScreenTotalArea().

    Changes from the Lawrence Dol version:

    • This avoids getting the graphics configuration twice.
    • Added a function for getting the total screen area.
    • Renamed the variables for clarity.
    • Added Javadocs.

    Code:

    /**
     * getScreenInsets, This returns the insets of the screen, which are defined by any task bars
     * that have been set up by the user. This function accounts for multi-monitor setups. If a
     * window is supplied, then the the monitor that contains the window will be used. If a window
     * is not supplied, then the primary monitor will be used.
     */
    static public Insets getScreenInsets(Window windowOrNull) {
        Insets insets;
        if (windowOrNull == null) {
            insets = Toolkit.getDefaultToolkit().getScreenInsets(GraphicsEnvironment
                    .getLocalGraphicsEnvironment().getDefaultScreenDevice()
                    .getDefaultConfiguration());
        } else {
            insets = windowOrNull.getToolkit().getScreenInsets(
                    windowOrNull.getGraphicsConfiguration());
        }
        return insets;
    }
    
    /**
     * getScreenWorkingArea, This returns the working area of the screen. (The working area excludes
     * any task bars.) This function accounts for multi-monitor setups. If a window is supplied,
     * then the the monitor that contains the window will be used. If a window is not supplied, then
     * the primary monitor will be used.
     */
    static public Rectangle getScreenWorkingArea(Window windowOrNull) {
        Insets insets;
        Rectangle bounds;
        if (windowOrNull == null) {
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            insets = Toolkit.getDefaultToolkit().getScreenInsets(ge.getDefaultScreenDevice()
                    .getDefaultConfiguration());
            bounds = ge.getDefaultScreenDevice().getDefaultConfiguration().getBounds();
        } else {
            GraphicsConfiguration gc = windowOrNull.getGraphicsConfiguration();
            insets = windowOrNull.getToolkit().getScreenInsets(gc);
            bounds = gc.getBounds();
        }
        bounds.x += insets.left;
        bounds.y += insets.top;
        bounds.width -= (insets.left + insets.right);
        bounds.height -= (insets.top + insets.bottom);
        return bounds;
    }
    
    /**
     * getScreenTotalArea, This returns the total area of the screen. (The total area includes any
     * task bars.) This function accounts for multi-monitor setups. If a window is supplied, then
     * the the monitor that contains the window will be used. If a window is not supplied, then the
     * primary monitor will be used.
     */
    static public Rectangle getScreenTotalArea(Window windowOrNull) {
        Rectangle bounds;
        if (windowOrNull == null) {
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            bounds = ge.getDefaultScreenDevice().getDefaultConfiguration().getBounds();
        } else {
            GraphicsConfiguration gc = windowOrNull.getGraphicsConfiguration();
            bounds = gc.getBounds();
        }
        return bounds;
    }
    
    0 讨论(0)
  • 2020-12-13 09:27
    Toolkit.getDefaultToolkit().getScreenSize().getWidth()
    
    0 讨论(0)
  • 2020-12-13 09:28

    A good way of detecting whether or not something is within visual bounds, is using

    Screen.getScreensForRectangle(x, y, width, height).isEmpty();
    
    0 讨论(0)
  • 2020-12-13 09:29

    The following code should do it (haven't tried it):

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();
    gd.getDefaultConfiguration().getBounds().getWidth();
    

    edit:

    For multiple monitors you should use the following code (taken from the javadoc of java.awt.GraphicsConfiguration:

      Rectangle virtualBounds = new Rectangle();
      GraphicsEnvironment ge = GraphicsEnvironment.
              getLocalGraphicsEnvironment();
      GraphicsDevice[] gs =
              ge.getScreenDevices();
      for (int j = 0; j < gs.length; j++) { 
          GraphicsDevice gd = gs[j];
          GraphicsConfiguration[] gc =
              gd.getConfigurations();
          for (int i=0; i < gc.length; i++) {
              virtualBounds =
                  virtualBounds.union(gc[i].getBounds());
          }
      } 
    
    0 讨论(0)
提交回复
热议问题