未呈现 JDialog 大小

JDialog Size Is Not Being Rendered

我创建了一个 JDialog 以保持 window 模态以阻止应用程序处理超过某个点。但是,当我使用这种方法时,弹出框,但大小没有设置为 400,400,它看起来好像没有大小。

public class test {

public static void main(String[] args) {
    JFrame theDog = new JFrame();
    JButton theButton = new JButton("hello");
    //theDog.setSize(200, 200);
    JDialog thadialog = new JDialog(theDog,"theTitle", Dialog.ModalityType.APPLICATION_MODAL);
    thadialog.setVisible(true);
    thadialog.setSize(400,400);
    thadialog.pack();


    System.out.println("hello");
}
}

如何使 JDialog 看起来大小为 400,400?

pack() 函数告诉 window 将自身包装得尽可能小以适合其组件。与调用 setSize() 函数相比,这是一种 不同的 大小设置方式。

通过调用 setSize()pack(),基本上你是在说 "first set your size to 400x400, then set your size to as small as possible."

换句话说,如果您希望 window 为特定大小,请坚持使用 setSize() 函数。摆脱对 pack().

的调用

编辑 - 您还需要在设置对话框大小之前将其设置为可见。由于您的对话框阻塞了当前线程,因此您甚至无法进入 setSize() 函数,直到 after 对话框关闭。在 setVisible().

调用 之前将对 setSize() 的调用移动到