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

有谁知道你会得到在Java中的屏幕宽度? 我读了一些关于工具箱方法的东西,但是我不太清楚那是什么东西。

谢谢你,安德鲁

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

这里是我使用的两个方法,它们占多个监视器和任务栏插页。 如果您不需要分别使用这两种方法,那么您当然可以避免两次获取graphicsconfiguration。

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

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

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

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

工具包有许多类可以帮助:

  1. getScreenSize – 原始屏幕大小
  2. getScreenInsets – 获取工具栏的大小,停靠
  3. getScreenResolution – dpi

我们最终使用1和2来计算可用的最大窗口大小。 为了得到相关的GraphicsConfiguration,我们使用

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

但可能会有更智能的多显示器解决scheme。

下面的代码应该这样做(还没有尝试过):

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

编辑:

对于多个监视器,您应该使用以下代码(从java.awt.GraphicsConfiguration的javadoc中获取 :

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

OP可能想要这样的东西:

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

你可以通过使用AWT Toolkit来获得它。

Toolkit.getScreenSize() 。

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

如果您需要当前分配给某个组件的屏幕分辨率(类似于根窗口的大部分内容),则可以使用此答案 。

检测是否在视觉范围内的一种好方法是使用

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

这是Lawrence Dol发表的多监视器解决scheme的改进。 正如在他的解决scheme中,这个代码占多个监视器和任务栏插页。 包含的函数是: getScreenInsets()getScreenWorkingArea()getScreenTotalArea()

Lawrence Dol版本的变化:

  • 这避免了两次获取graphicsconfiguration。
  • 增加了获取总屏幕区域的function。
  • 为了清楚起见,将variables重命名。
  • 增加了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; }