2010-09-09 42 views
109

Làm cách nào để có được độ phân giải màn hình (chiều rộng x chiều cao) tính bằng pixel?Làm thế nào tôi có thể nhận được độ phân giải màn hình bằng java?

Tôi đang sử dụng phương pháp JFrame và java swing.

+2

bạn có thể cung cấp thêm một số chi tiết về những gì bạn đặt câu hỏi. Lớp lót có thể dẫn đến hàng trăm cách khác nhau. –

+7

Tôi đoán bạn không quan tâm đến nhiều thiết lập màn hình. Có vẻ như nhiều nhà phát triển ứng dụng bỏ qua những điều này. Mọi người đều sử dụng nhiều màn hình, nơi tôi làm việc, vì vậy chúng tôi luôn phải suy nghĩ về họ. Chúng tôi thăm dò tất cả các màn hình và đặt chúng thành các đối tượng màn hình để chúng tôi có thể nhắm mục tiêu chúng khi chúng tôi mở các khung hình mới. Nếu bạn thực sự không cần chức năng này, thì tôi đoán là bạn đã hỏi câu hỏi mở như vậy và chấp nhận câu trả lời quá nhanh. –

Trả lời

214

Bạn có thể nhận kích thước màn hình bằng phương pháp Toolkit.getScreenSize().

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

Trên một cấu hình đa màn hình, bạn nên sử dụng này:

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

Nếu bạn muốn để có được độ phân giải màn hình trong DPI bạn sẽ phải sử dụng phương pháp getScreenResolution() trên Toolkit.


Resources:

+0

Điều này không hiệu quả đối với tôi. Tôi có một màn hình 3840x2160, nhưng 'getScreenSize' trả về 1920x1080. – ZhekaKozlov

11

Cuộc gọi này sẽ cung cấp cho bạn thông tin bạn muốn.

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

System.out.println(resolution); 
15

Mã này sẽ liệt kê các thiết bị đồ họa trên hệ thống (nếu nhiều màn hình được cài đặt), và bạn có thể sử dụng thông tin đó để xác định sở thích hoặc vị trí tự động của màn hình (một số hệ thống sử dụng màn hình nhỏ để hiển thị thời gian thực trong khi ứng dụng đang chạy ở chế độ nền và màn hình như vậy có thể được xác định theo kích thước, màu màn hình, v.v.):

// 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

Đây là độ phân giải của màn hình mà thành phần đã cho hiện được gán (giống như hầu hết các phần của cửa sổ gốc hiển thị trên màn hình đó).

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

Cách sử dụng:

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 

Nếu bạn muốn tôn trọng các thanh công cụ, vv bạn sẽ cần phải tính toán với điều này, quá:

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

Dưới đây là một số mã chức năng (Java 8) trả về vị trí x của cạnh phải nhất của màn hình bên phải nhất. Nếu không tìm thấy màn hình nào, khi đó màn hình sẽ trả về 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); 

Đây là các liên kết tới JavaDoc.

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

1

Ba chức năng trả lại kích thước màn hình trong Java. Mã này chiếm các thiết lập và thanh tác vụ đa màn hình. Các chức năng được bao gồm là: getScreenInsets(), getScreenWorkingArea()getScreenTotalArea().

Code:

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

Chào mừng bạn đến với Stack Overflow! Mặc dù đoạn mã này có thể giải quyết được câu hỏi, [bao gồm cả giải thích] (// meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) thực sự giúp cải thiện chất lượng bài đăng của bạn. Hãy nhớ rằng bạn đang trả lời câu hỏi cho người đọc trong tương lai và những người đó có thể không biết lý do cho đề xuất mã của bạn. Ngoài ra, vui lòng không cố gắng gắn mã của bạn với các nhận xét giải thích, điều này làm giảm khả năng đọc của cả mã và giải thích! – kayess

1

Dưới đây là một đoạn mã tôi thường sử dụng. Nó trả về vùng màn hình có sẵn đầy đủ (ngay cả trên các thiết lập đa màn hình) trong khi vẫn giữ lại các vị trí giám sát gốc.

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

Trên một máy tính với hai màn hình full-HD, nơi mà một trái được thiết lập như màn hình chính (trong cài đặt Windows), hàm trả về

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

Trên thiết lập tương tự, nhưng với màn hình bên phải được đặt làm màn hình chính, chức năng trả về

java.awt.Rectangle[x=-1920,y=0,width=3840,height=1080] 
Các vấn đề liên quan