2010-09-09 34 views
109

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

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

+2

你能提供一些关于你提出的问题的更多细节。一个班轮可以导致数百种不同的方式。 – 2010-09-09 20:17:25

+7

我想你不关心多个显示器的设置。似乎很多应用程序开发人员忽略了这些。在我工作的地方,每个人都使用多台显示器,所以我们总是需要考虑他们。我们探测所有监视器并将它们设置为屏幕对象,以便我们可以在打开新框架时将其定位。如果你真的不需要这个功能,那么我想你可以问这样一个开放式的问题并且很快接受了答案。 – 2010-09-09 20:50:52

回答

214

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

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

在多显示器配置,你应该这样做:

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

如果你想在DPI屏幕分辨率,你将不得不使用的方法getScreenResolution()Toolkit


资源:

+0

这对我不起作用。我有一个3840x2160监视器,但getScreenSize返回1920x1080。 – ZhekaKozlov 2018-03-01 10:25:32

11

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

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
1
int resolution =Toolkit.getDefaultToolkit().getScreenResolution(); 

System.out.println(resolution); 
15

此代码将枚举系统上的图形装置(如果安装了多个监视器),和你可以使用这些信息确定显示器亲和力或自动放置(某些系统使用侧面监视器进行实时显示,而应用程序在后台运行,并且此类显示器可通过大小,屏幕颜色等进行标识):

// 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 
} 
1
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); 
3

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

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); 
2

这里的一些功能代码(Java的8 ),它返回最右边屏幕最右边的x位置。如果未找到屏幕,则返回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()

1

这三个函数在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; 
} 
0
int screenResolution = Toolkit.getDefaultToolkit().getScreenResolution(); 
System.out.println(""+screenResolution); 
+0

欢迎来到Stack Overflow!尽管这段代码可以解决这个问题,但[包括一个解释](// meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)确实有助于提高您的帖子的质量。请记住,您将来会为读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要用解释性注释来挤占代码,这会降低代码和解释的可读性! – kayess 2016-12-20 14:43:12

1

下面是一个代码片段,我经常使用。它会返回完整的可用屏幕区域(即使在多显示器设置中),同时保留原生监视器位置。

public static Rectangle getMaximumScreenBounds() { 
    int minx=0, miny=0, maxx=0, maxy=0; 
    GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
    for(GraphicsDevice device : environment.getScreenDevices()){ 
     Rectangle bounds = device.getDefaultConfiguration().getBounds(); 
     minx = Math.min(minx, bounds.x); 
     miny = Math.min(miny, bounds.y); 
     maxx = Math.max(maxx, bounds.x+bounds.width); 
     maxy = Math.max(maxy, bounds.y+bounds.height); 
    } 
    return new Rectangle(minx, miny, maxx-minx, maxy-miny); 
} 

在有两个全高清显示器,其中左侧是设置为主显示器(在Windows中设置)的计算机,该函数返回

java.awt.Rectangle[x=0,y=0,width=3840,height=1080] 

在相同的设置,但将正确的显示器设置为主显示器,功能返回

java.awt.Rectangle[x=-1920,y=0,width=3840,height=1080]