你如何获得java中的屏幕宽度?

时间:2009-12-20 18:40:11

标签: java swing

有谁知道如何在java中获得屏幕宽度?我读了一些关于某些工具包方法的内容,但我不太清楚它是什么。

谢谢, 安德鲁

12 个答案:

答案 0 :(得分:77)

java.awt.Toolkit.getDefaultToolkit().getScreenSize()

答案 1 :(得分:18)

以下是我使用的两种方法,它们考虑了多个监视器和任务栏插件。如果您不需要单独使用这两种方法,当然可以避免两次使用图形配置。

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

答案 2 :(得分:16)

工作区域是显示器的桌面区域,不包括任务栏,停靠窗口和停靠工具栏。

如果你想要的是屏幕的“工作区域”,请使用:

public static int GetScreenWorkingWidth() {
    return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width;
}

public static int GetScreenWorkingHeight() {
    return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height;
}

答案 3 :(得分:5)

以下代码应该这样做(尚未尝试过):

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

修改

对于多个监视器,您应该使用以下代码(取自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());
      }
  } 

答案 4 :(得分:5)

Toolkit有许多类可以提供帮助:

  1. getScreenSize - 原始屏幕尺寸
  2. getScreenInsets - 获取工具栏的大小,停靠
  3. getScreenResolution - dpi
  4. 我们最终使用1和2来计算可用的最大窗口大小。要获得相关的GraphicsConfiguration,我们使用

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

    但可能有更智能的多显示器解决方案。

答案 5 :(得分:2)

OP可能想要这样的东西:

Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();

答案 6 :(得分:1)

Toolkit.getDefaultToolkit().getScreenSize().getWidth()

答案 7 :(得分:0)

您可以使用AWT Toolkit

来获取它

答案 8 :(得分:0)

Toolkit.getScreenSize()

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

答案 9 :(得分:0)

如果您需要当前指定某个组件的屏幕分辨率(在该屏幕上可以看到根窗口的大部分内容),您可以使用this answer

答案 10 :(得分:0)

检测某些东西是否在视觉范围内的好方法是使用

Screen.getScreensForRectangle(x, y, width, height).isEmpty();

答案 11 :(得分:0)

这是Lawrence Dol发布的多显示器解决方案(上图)的改进。在他的解决方案中,此代码考虑了多个监视器和任务栏插件。包含的函数包括: getScreenInsets() getScreenWorkingArea() getScreenTotalArea()

Lawrence Dol版本的变化:

  • 这可以避免两次获得图形配置。
  • 添加了获取总屏幕区域的功能。
  • 为了清晰起见,重命名了变量。
  • 添加了Javadocs。

代码:

/**
 * 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;
}