Get location of a swing component

前端 未结 6 1292
野的像风
野的像风 2021-01-18 03:19

I have put some JPanels into another JPanel which its\' layout is Box Layout and Y-Axis. After I have put all the panels I need to get the Y position of each added JPanel in

6条回答
  •  抹茶落季
    2021-01-18 04:17

    Suppose you have Jpanel father that contains a JPanel son. If cx is the coordinate x of the son with respect to the father and cy is the coordinate y of the son with respect to the father, then:

    cx = son.getLocationOnScreen().getX()-father.getLocationOnScreen().getX()

    cy = son.getLocationOnScreen().getY()-father.getLocationOnScreen().getY()

    I think is the easiest way...

提交回复
热议问题