java获取组件位置,在Java中获取组件的坐标

I have a JLayeredPane, and inside the JLayeredPane is a JInternalFrame. What I need are the bounds (x position, y position, width, height) of the content pane in the JInternalFrame. The bounds need to be in relation to the JLayeredPane. My problem is the borders, title bar, and Im not sure what else of the JInternalFrame are messing with my calculation. Its a few pixels off.

Here is how I try to calculate it. This code is in the 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

);

I need to get the location of the content pane of the internal frame.

解决方案

Coordinates with respect to what? The screen? The window?

Calling getLocation() will give you the coordinates relative to the components parent in the window hierarchy.

SwingUtilities has several methods for transforming coordinates from one frame of reference to another. So, to compensate for the title bar and frame, you can do this:

JInternalFrame iframe = ...

Container c = iframe.getContentPane();

Rectangle r = c.getBounds();

r = SwingUtilities.convertRectangle(c.getParent(), r, iframe.getParent());

I have a JLayeredPane, and inside the JLayeredPane is a JInternalFrame. What I need are the bounds (x position, y position, width, height) of the content pane in the JInternalFrame. The bounds need to be in relation to the JLayeredPane. My problem is the borders, title bar, and Im not sure what else of the JInternalFrame are messing with my calculation. Its a few pixels off. Here is how I try to calculate it. This code is in the 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 ); I need to get the location of the content pane of the internal frame. 解决方案 Coordinates with respect to what? The screen? The window? Calling getLocation() will give you the coordinates relative to the components parent in the window hierarchy. SwingUtilities has several methods for transforming coordinates from one frame of reference to another. So, to compensate for the title bar and frame, you can do this: JInternalFrame iframe = ... Container c = iframe.getContentPane(); Rectangle r = c.getBounds(); r = SwingUtilities.convertRectangle(c.getParent(), r, iframe.getParent());
经验分享 程序员 微信小程序 职场和发展