Set location of JDialog relative to JFrame

后端 未结 3 2047
旧时难觅i
旧时难觅i 2021-02-04 13:38

Is there a way to set a dialog location relative to a JFrame?

I would like to center the dialog to the frame that houses my GUI, instead the dialog often a

3条回答
  •  Happy的楠姐
    2021-02-04 13:52

    You should give this as the argument for the parent parameter when you construct the dialog (when you call it from a JFrame class that is usually your mainform). Unless you give code in your question, I cannot give more detailed help...

    Edit: To center it within the parent, do this:

     MyDialog dialog = new MyDialog(this, true); //or false for non-modal
     dialog.setLocation(this.getWidth/2 - dialog.getWidth/2, this.getHeight/2 - dialog.getHeight/2);
     dialog.setVisible(true);
    

提交回复
热议问题