回答
您可以使用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
。
資源:
這對我不起作用。我有一個3840x2160監視器,但getScreenSize返回1920x1080。 – ZhekaKozlov 2018-03-01 10:25:32
這個電話會給你你想要的信息。
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int resolution =Toolkit.getDefaultToolkit().getScreenResolution();
System.out.println(resolution);
此代碼將枚舉系統上的圖形裝置(如果安裝了多個監視器),和你可以使用這些信息確定顯示器親和力或自動放置(某些系統使用側面監視器進行實時顯示,而應用程序在後臺運行,並且此類顯示器可通過大小,屏幕顏色等進行標識):
// 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();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
framemain.setSize((int)width,(int)height);
framemain.setResizable(true);
framemain.setExtendedState(JFrame.MAXIMIZED_BOTH);
這是給定組件當前分配的屏幕分辨率(類似於根窗口的大部分部分在該屏幕上可見)。
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位置。如果未找到屏幕,則返回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()
這三個函數在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);
歡迎來到Stack Overflow!儘管這段代碼可以解決這個問題,但[包括一個解釋](// meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)確實有助於提高您的帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。也請儘量不要用解釋性註釋來擠佔代碼,這會降低代碼和解釋的可讀性! – kayess 2016-12-20 14:43:12
下面是一個代碼片段,我經常使用。它會返回完整的可用屏幕區域(即使在多顯示器設置中),同時保留原生監視器位置。
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]
- 1. 如何獲得屏幕分辨率R
- 2. 如何在java中獲得屏幕分辨率?
- 3. 獲取屏幕分辨率
- 4. 如何在C++中獲得屏幕分辨率?
- 5. 如何在ActionScript 3中獲得用戶的屏幕分辨率?
- 6. 屏幕分辨率
- 7. 屏幕分辨率
- 8. 屏幕分辨率
- 9. 屏幕分辨率
- 10. 屏幕分辨率
- 11. 如何從hWnd獲得顯示器屏幕分辨率?
- 12. 如何獲得黑莓屏幕分辨率?
- 13. 如何獲得顯示分辨率(屏幕尺寸)?
- 14. 如何獲得用戶的屏幕分辨率與PHP
- 15. 如何獲得設備(Windows Phone)的屏幕分辨率
- 16. 在XNA中獲得支持的屏幕分辨率?
- 17. 如何閱讀屏幕分辨率 - 並更改此分辨率?
- 18. Java屏幕分辨率變化
- 19. 屏幕分辨率Java代碼
- 20. Java屏幕分辨率大小
- 21. ... Java的屏幕分辨率更改
- 22. 如何獲得Java g.drawString()高分辨率?
- 23. Qt屏幕分辨率初始屏幕
- 24. 屏幕分辨率和CSS
- 25. 更改屏幕分辨率
- 26. 標誌,屏幕分辨率
- 27. Android屏幕分辨率
- 28. Silverlight屏幕分辨率
- 29. Android:Force 480x320屏幕分辨率
- 30. 屏幕分辨率低,xamarin
你能提供一些關於你提出的問題的更多細節。一個班輪可以導致數百種不同的方式。 – 2010-09-09 20:17:25
我想你不關心多個顯示器的設置。似乎很多應用程序開發人員忽略了這些。在我工作的地方,每個人都使用多臺顯示器,所以我們總是需要考慮他們。我們探測所有監視器並將它們設置爲屏幕對象,以便我們可以在打開新框架時將其定位。如果你真的不需要這個功能,那麼我想你可以問這樣一個開放式的問題並且很快接受了答案。 – 2010-09-09 20:50:52