我怎样才能获得在Java中的屏幕分辨率?

如何获得像素的屏幕分辨率(宽度x高度)?

我正在使用JFrame和java swing方法。

9 Solutions collect form web for “我怎样才能获得在Java中的屏幕分辨率?”

您可以使用Toolkit.getScreenSize()方法获得屏幕大小。

 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); double width = screenSize.getWidth(); double height = screenSize.getHeight(); 

在多监视器configuration上,你应该使用这个:

 GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); int width = gd.getDisplayMode().getWidth(); int height = gd.getDisplayMode().getHeight(); 

如果您想在DPI中获得屏幕分辨率,则必须使用Toolkit上的getScreenResolution()方法。


资源:

  • javadoc – Toolkit.getScreenSize()
  • Java bug 5100801- Toolkit.getScreenSize()在multimon,linux上不返回正确的维度

此代码将枚举系统上的graphics设备(如果安装了多个监视器),并且可以使用该信息来确定监视器的亲和性或自动放置(某些系统在运行应用程序时使用一个侧面监视器用于实时显示背景,以及这样的显示器可以通过尺寸,屏幕颜色等来标识):

 // Test if each monitor will support my app's window // Iterate through each monitor and see what size each is GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice[] gs = ge.getScreenDevices(); Dimension mySize = new Dimension(myWidth, myHeight); Dimension maxSize = new Dimension(minRequiredWidth, minRequiredHeight); for (int i = 0; i < gs.length; i++) { DisplayMode dm = gs[i].getDisplayMode(); if (dm.getWidth() > maxSize.getWidth() && dm.getHeight() > maxSize.getHeight()) { // Update the max size found on this monitor maxSize.setSize(dm.getWidth(), dm.getHeight()); } // Do test if it will work here } 

这个电话会给你你想要的信息。

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

这是给定组件当前分配的屏幕分辨率(类似于根窗口的大部分部分在该屏幕上可见)。

 public Rectangle getCurrentScreenBounds(Component component) { return component.getGraphicsConfiguration().getBounds(); } 

用法:

 Rectangle currentScreen = getCurrentScreenBounds(frameOrWhateverComponent); int currentScreenWidth = currentScreen.width // current screen width int currentScreenHeight = currentScreen.height // current screen height // absolute coordinate of current screen > 0 if left of this screen are further screens int xOfCurrentScreen = currentScreen.x 

如果你想尊重工具栏等,你也需要用这个来计算:

 GraphicsConfiguration gc = component.getGraphicsConfiguration(); Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(gc); 

这里有一些函数代码(Java 8),它返回最右边屏幕右边最右边的x位置。 如果没有find屏幕,则返回0。

  GraphicsDevice devices[]; devices = GraphicsEnvironment. getLocalGraphicsEnvironment(). getScreenDevices(); return Stream. of(devices). map(GraphicsDevice::getDefaultConfiguration). map(GraphicsConfiguration::getBounds). mapToInt(bounds -> bounds.x + bounds.width). max(). orElse(0); 

这里是JavaDoc的链接。

GraphicsEnvironment.getLocalGraphicsEnvironment()
GraphicsEnvironment.getScreenDevices()
GraphicsDevice.getDefaultConfiguration()
GraphicsConfiguration.getBounds()

 int resolution =Toolkit.getDefaultToolkit().getScreenResolution(); System.out.println(resolution); 
 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); double width = screenSize.getWidth(); double height = screenSize.getHeight(); framemain.setSize((int)width,(int)height); framemain.setResizable(true); framemain.setExtendedState(JFrame.MAXIMIZED_BOTH); 

这三个函数返回Java中的屏幕大小。 这个代码考虑了多监视器设置和任务栏。 包含的函数是: getScreenInsets()getScreenWorkingArea()getScreenTotalArea()

码:

 /** * 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; } 
 int screenResolution = Toolkit.getDefaultToolkit().getScreenResolution(); System.out.println(""+screenResolution); 
  • 以编程方式在Java Swing中单击GUIbutton
  • 设置jLabel的打印尺寸并在打印件上放置jRadiobutton
  • 保持文本检索的正确风格
  • 如何在Swing java中创build一个JFrame模式
  • 在paintbrushtypes的GUI中,paintComponent()与paint()和JPanel vs Canvas
  • Java和GUI - 根据MVC模式,ActionListeners属于哪里?
  • 如何绘制代表连接节点图的树?
  • 与圆angular和透明度的边界
  • 如何添加JRadioButton在JTable组中
  • 如何使用java“打开”和“保存”
  • 透明的JButton