2012-04-12 41 views
20

Tôi muốn nhận được kích thước màn hình hiệu quả. Đó là: kích thước của màn hình mà không có thanh tác vụ (hoặc tương đương trên Linux/Mac).Nhận kích thước màn hình hiệu quả từ Java

Tôi hiện đang sử dụng ...

component.getGraphicsConfiguration().getBounds() 

... và trừ kích thước thanh công cụ mặc định tùy thuộc vào hệ điều hành, nhưng tôi muốn một cách mà làm việc ngay cả khi người dùng đã thay đổi kích cỡ/di chuyển thanh tác vụ .

Trả lời

37

Điều này có thể xác định kích thước màn hình bằng pixel mà không có thanh tác vụ

//size of the screen 
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 

//height of the task bar 
Insets scnMax = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration()); 
int taskBarSize = scnMax.bottom; 

//available size of the screen 
setLocation(screenSize.width - getWidth(), screenSize.height - taskBarSize - getHeight()); 

EDIT

Có thể ai đó hãy chạy mã này vào Xx_nix và Mac OSX và kiểm tra xem JDialog thực sự là đặt ở góc dưới bên phải ?

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.UIManager.LookAndFeelInfo; 

public class NotificationPopup { 

    private static final long serialVersionUID = 1L; 
    private LinearGradientPaint lpg; 
    private JDialog dialog = new JDialog(); 
    private BackgroundPanel panel = new BackgroundPanel(); 

    public NotificationPopup() { 
     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
     Insets scnMax = Toolkit.getDefaultToolkit(). 
       getScreenInsets(dialog.getGraphicsConfiguration()); 
     int taskBarSize = scnMax.bottom; 
     panel.setLayout(new GridBagLayout()); 
     GridBagConstraints constraints = new GridBagConstraints(); 
     constraints.gridx = 0; 
     constraints.gridy = 0; 
     constraints.weightx = 1.0f; 
     constraints.weighty = 1.0f; 
     constraints.insets = new Insets(5, 5, 5, 5); 
     constraints.fill = GridBagConstraints.BOTH; 
     JLabel l = new JLabel("You have got 2 new Messages."); 
     panel.add(l, constraints); 
     constraints.gridx++; 
     constraints.weightx = 0f; 
     constraints.weighty = 0f; 
     constraints.fill = GridBagConstraints.NONE; 
     constraints.anchor = GridBagConstraints.NORTH; 
     JButton b = new JButton(new AbstractAction("x") { 

      private static final long serialVersionUID = 1L; 

      @Override 
      public void actionPerformed(final ActionEvent e) { 
       dialog.dispose(); 
      } 
     }); 
     b.setOpaque(false); 
     b.setMargin(new Insets(1, 4, 1, 4)); 
     b.setFocusable(false); 
     panel.add(b, constraints); 
     dialog.setUndecorated(true); 
     dialog.setSize(300, 100); 
     dialog.setLocation(screenSize.width - dialog.getWidth(), 
       screenSize.height - taskBarSize - dialog.getHeight()); 
     lpg = new LinearGradientPaint(0, 0, 0, dialog.getHeight()/2, 
       new float[]{0f, 0.3f, 1f}, new Color[]{new Color(0.8f, 0.8f, 1f), 
        new Color(0.7f, 0.7f, 1f), new Color(0.6f, 0.6f, 1f)}); 
     dialog.setContentPane(panel); 
     dialog.setVisible(true); 
    } 

    private class BackgroundPanel extends JPanel { 

     private static final long serialVersionUID = 1L; 

     BackgroundPanel() { 
      setOpaque(true); 
     } 

     @Override 
     protected void paintComponent(final Graphics g) { 
      final Graphics2D g2d = (Graphics2D) g; 
      g2d.setPaint(lpg); 
      g2d.fillRect(1, 1, getWidth() - 2, getHeight() - 2); 
      g2d.setColor(Color.BLACK); 
      g2d.drawRect(0, 0, getWidth() - 1, getHeight() - 1); 
     } 
    } 

    public static void main(final String[] args) { 
     try { 
      for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { 
       System.out.println(info.getName()); 
       if ("Nimbus".equals(info.getName())) { 
        UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (UnsupportedLookAndFeelException e) { 
     } catch (ClassNotFoundException e) { 
     } catch (InstantiationException e) { 
     } catch (IllegalAccessException e) { 
     } 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 

       NotificationPopup notificationPopup = new NotificationPopup(); 
      } 
     }); 
    } 
} 
+0

Có vẻ tốt trên Ubuntu/OpenJDK. –

+1

Nó có thể có thể được cố định, nhưng có vẻ như điều này sẽ không hoạt động trong một thiết lập đa màn hình. –

+0

có hai cấp độ, có nghĩa là gì ??? '1)' một container chứa hai hoặc nhiều multi_monitors (không được thử nghiệm trong Java7, nhưng Java6 không được hỗ trợ), hoặc '2)' một thùng chứa được đặt trên một trong các multi_monitors (được trả lời bởi alain.janinm), – mKorbel

51

GraphicsEnvironment có một phương thức trả về kích thước tối đa có sẵn, chiếm tất cả taskbars, vv bất kể họ đang liên kết:

GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds() 

Lưu ý: Trên hệ thống đa màn hình, getMaximumWindowBounds() trả về giới hạn của toàn bộ khu vực hiển thị. Để nhận các giới hạn có thể sử dụng của một màn hình, hãy sử dụng GraphicsConfiguration.getBounds()Toolkit.getScreenInsets() như được hiển thị trong các câu trả lời khác.

+2

Trong trường hợp của tôi, nó trả về toàn bộ chiều cao. (khi thanh tác vụ trong suốt và cửa sổ có thể xuống dưới) – Lukino

10

Đây là mã tôi đã kết thúc bằng:

GraphicsConfiguration gc = // ... 

Rectangle bounds = gc.getBounds(); 

Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(gc); 

Rectangle effectiveScreenArea = new Rectangle(); 

effectiveScreenArea.x = bounds.x + screenInsets.left; 
effectiveScreenArea.y = bounds.y + screenInsets.top; 
effectiveScreenArea.height = bounds.height - screenInsets.top - screenInsets.bottom;   
effectiveScreenArea.width = bounds.width - screenInsets.left - screenInsets.right; 
+1

@mKorbel: Tôi đã thử nghiệm trên Ubuntu, OS X và Windows 7. Nó sẽ được thử nghiệm trên một số nền tảng khác khi ứng dụng di chuyển để kiểm tra (và tôi chắc chắn sẽ lưu ý ở đây, nếu chúng ta gặp phải vấn đề ở bất cứ đâu). –

2

Dưới đây là một phương pháp tôi đã viết để nhanh chóng thực hiện các phép tính bằng cách trừ đi lợi nhuận và trung nó trong màn hình.

public void setToEffectiveScreenSize() { 
    double width, height, x, y; 

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
    Insets bounds = Toolkit.getDefaultToolkit().getScreenInsets(frmMain.getGraphicsConfiguration()); 

    // Calculate the height/length by subtracting the margins 
    // (x,y) = ((screenHeight-windowHeight)/2, (screenWidth - windowWidth)/2) 

    width = screenSize.getWidth() - bounds.left - bounds.right; 
    height = screenSize.getHeight() - bounds.top - bounds.bottom; 

    // Now center the new rectangle inside the screen 
    x = (screenSize.getHeight() - height)/2.0; 
    y = (screenSize.getWidth() - width)/2.0; 

    frmMain.setBounds((int)x,(int)y,(int)width,(int)height); 
} 
Các vấn đề liên quan