How to set the JFrame as a parent to the JDialog

前端 未结 1 637
Happy的楠姐
Happy的楠姐 2021-01-17 10:43

I am having trouble to set the frame as a owner to the dialog. Normally when I extend JDialog class for creating a dialog then I use super(frame) t

相关标签:
1条回答
  • 2021-01-17 11:18

    A dialog's (or window's) owner can be set only in the constructor, so the only way to set it is by using a constructor which takes the owner as parameter, like:

    class DialogWithoutExtend {
    
        private JFrame frame;
        public DialogWithoutExtend(JFrame frame) {
            this.frame = frame;
        }
    
        public void cretaUI() {
            JDialog dialog = new JDialog(frame);
            dialog.setTitle("Dialog created without extending JDialog class.");
            dialog.setSize(new Dimension(400, 100));
            dialog.setLocationRelativeTo(frame);
            dialog.setModal(true);
            dialog.setVisible(true);
        }
    }
    
    0 讨论(0)
提交回复
热议问题