2009-12-20 24 views
40

Có ai biết làm thế nào bạn sẽ có được chiều rộng màn hình trong java? Tôi đã đọc một số điều về một số phương pháp bộ công cụ nhưng tôi không hoàn toàn chắc chắn đó là gì.Làm thế nào để bạn có được chiều rộng màn hình trong java?

Cảm ơn, Andrew

+5

Lưu ý, cẩn thận phải được thực hiện khi có nhiều màn hình. –

+1

Bạn cũng PHẢI chiếm tài khoản cho màn hình inset (xem câu trả lời của tôi); nhiều người thích di chuyển thanh tác vụ sang một bên hoặc bên kia màn hình. –

Trả lời

77
java.awt.Toolkit.getDefaultToolkit().getScreenSize() 
+0

McCan: trả về? – Hydroid

+1

@Hydroid http://stackoverflow.com/a/8101318/632951 – Pacerier

+0

Trả lại đối tượng thuộc loại 'Thứ nguyên' – KJaeg

5

Các mã sau đây nên làm điều đó (đã không thử nó):

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

chỉnh sửa:

Đối với nhiều màn hình, bạn nên sử dụng mã sau đây (được lấy từ javadoc of java.awt.GraphicsConfiguration:

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

Lấy đẹp. Đã tìm kiếm điều này trong nhiều ngày, không thể tìm thấy tiêu chí tìm kiếm phù hợp trên SO. –

2

Các OP lẽ muốn một cái gì đó như thế này:

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

Toolkit có một số lớp học mà có thể giúp:

  1. getScreenSize - màn hình nguyên kích thước
  2. getScreenInsets - có kích thước thanh công cụ , Bến
  3. getScreenResolution - dpi

Chúng tôi sẽ chỉ sử dụng 1 và 2, để tính toán có thể sử dụng kích thước cửa sổ tối đa. Để có GraphicsConfiguration liên quan, chúng tôi sử dụng

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

nhưng có thể có các giải pháp thông minh hơn nhiều màn hình.

+0

là mục đầu tiên trong mảng 'getScreenDevices' ** được bảo đảm ** là màn hình chính? – Pacerier

+0

Không được chỉ định trong API - tốt hơn để đi với getDefaultScreenDevice? http://download.oracle.com/javase/6/docs/api/java/awt/GraphicsEnvironment.html#getDefaultScreenDevice() –

+0

Bạn có nghĩa là 'GraphicsEnvironment.getDefaultScreenDevice()'? – Pacerier

18

Dưới đây là hai phương pháp tôi sử dụng, tài khoản này cho nhiều màn hình và thanh công cụ. Nếu bạn không cần hai phương pháp riêng biệt, bạn có thể, tất nhiên, tránh nhận được cấu hình đồ họa hai lần.

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

+1 để hiển thị cách lấy mẫu. Chúng có thể quan trọng, đặc biệt là trên OS X nếu bạn muốn có một cửa sổ kích thước đầy đủ mà không cần chồng chéo lên dock. –

+0

@SoftwareMonkey là có anyway để có được các graphicsConfiguration mà không cần phải tạo một Frame mới? – Pacerier

+0

-1: không có 'Toolkit.getDefaultToolkit(). GetGraphicsConfiguration()' – jan

16

Khu vực làm việc là khu vực màn hình của màn hình, không bao gồm thanh tác vụ, cửa sổ được gắn và thanh công cụ được gắn đế.

Nếu những gì bạn muốn là "khu vực làm việc" của màn hình, sử dụng này:

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

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

điều này không hoạt động cho các tình huống đa màn hình – jan

0

Nếu bạn cần độ phân giải của màn hình mà một thành phần nào đó hiện đang được giao (một cái gì đó giống như hầu hết phần của cửa sổ gốc hiển thị trên màn hình đó), bạn có thể sử dụng this answer.

0

Một cách tốt để phát hiện hay không một cái gì đó nằm trong giới hạn hình ảnh, đang sử dụng

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

Đây là một sự cải tiến các giải pháp đa màn hình đăng (ở trên) bởi Lawrence Dol. Như trong giải pháp của mình, mã này chiếm nhiều màn hình và thanh tác vụ thanh tác vụ. Các chức năng được bao gồm là: getScreenInsets(), getScreenWorkingArea()getScreenTotalArea().

thay đổi từ phiên bản Lawrence Dol:

  • Điều này tránh việc cấu hình đồ họa gấp đôi.
  • Đã thêm chức năng để nhận tổng diện tích màn hình.
  • Đã đổi tên các biến để rõ ràng.
  • Đã thêm Javadocs.

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; 
} 
Các vấn đề liên quan