在java中获取组件的坐标

use*_*041 2 java swing coordinates

我有一个JLayeredPane,而JLayeredPane内部是一个JInternalFrame.我需要的是JInternalFrame中内容窗格的边界(x位置,y位置,宽度,高度).边界需要与JLayeredPane相关.我的问题是边框,标题栏,我不确定JInternalFrame的其他内容是否正在弄乱我的计算.它只有几个像素.

这是我尝试计算它的方式.此代码位于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
                );

我需要获取内部框架的内容窗格的位置. 替代文字

Dev*_*ler 6

协调什么?屏幕?窗户?

调用getLocation()将为您提供窗口层次结构中相对于组件父级的坐标.

SwingUtilities有几种方法可以将坐标从一个参考帧转换为另一个参考帧.因此,要补偿标题栏和框架,您可以这样做:

JInternalFrame iframe = ...
Container c = iframe.getContentPane();
Rectangle r = c.getBounds();
r = SwingUtilities.convertRectangle(c.getParent(), r, iframe.getParent());
Run Code Online (Sandbox Code Playgroud)