Center JDialog over parent

前端 未结 5 706
借酒劲吻你
借酒劲吻你 2020-11-30 09:39

I have a Java swing application with a button that produces a popup window when a certain action is performed. I\'d like to align the center point of the popup window with

5条回答
  •  执念已碎
    2020-11-30 10:16

    On the JDialog you've created you should call pack() first, then setLocationRelativeTo(parentFrame), and then setVisible(true). With that order the JDialog should appear centered on the parent frame.

    If you don't call pack() first, then setting the location relative to the parent doesn't work properly because the JDialog doesn't know what size it is at that point. It appears to take the size as 0 by 0, which results in the "top left pixel of the popup over the center pixel of the parent" positioning mentioned in a comment to one of the other answers.

提交回复
热议问题