2010-11-24 47 views
2

我有一個JLayeredPane,並且JLayeredPane內部是一個JInternalFrame。我需要的是JInternalFrame中內容窗格的邊界(x位置,y位置,寬度,高度)。界限需要與JLayeredPane相關。我的問題是邊框,標題欄,我不確定JInternalFrame還有什麼與我的計算相混淆。這是幾個像素關閉。在java中獲取組件座標

下面是我如何計算它。此代碼位於JInternalFrame中:

 

Rectangle area = new Rectangle(
       getX() + 2, //The x coordinate of the JInternalFrame + 2 for the border 
       getY() + ((javax.swing.plaf.basic.BasicInternalFrameUI) getUI()).getNorthPane().getHeight(), //The Y position of the JinternalFrame + theheight of the title bar of the JInternalFrame 
       getContentPane().getWidth() - 2, //The width of the Jinternals content pane - the border of the frame 
       getContentPane().getHeight() //the height of the JinternalFrames content pane 
       ); 
 

我需要獲取內部框架內容窗格的位置。 alt text

回答

6

關於什麼座標?屏幕?窗戶?

調用getLocation()將爲您提供窗口層次結構中相對於組件父項的座標。

SwingUtilities有幾種將座標從一個參照系轉換到另一個參照系的方法。所以,爲了彌補標題欄和框架,你可以這樣做:

JInternalFrame iframe = ... 
Container c = iframe.getContentPane(); 
Rectangle r = c.getBounds(); 
r = SwingUtilities.convertRectangle(c.getParent(), r, iframe.getParent()); 
+0

非常酷。我知道有一個簡單的方法來做到這一點。這工作完美。 – user489041 2010-11-24 19:49:26

2

JInternalFrame的getBounds()方法返回一個具有所需座標的Rectangle。爲什麼不只是做到以下幾點:

{ 
     ... 
     JInternalFrame iframe = ...; 
     Rectangle r = new Rectangle(iframe.getBounds()); 
} 

這樣你就不需要手動計算邊界。

+0

問題是這會給我的界限,包括內部框架的標題欄和邊框。我只需要內容窗格的邊界。但我需要x座標和y座標到JLayeredPane中內容窗格的位置。 – user489041 2010-11-24 19:48:53