Java Swing:为 JDialog 设置固定的窗口大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20293220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swing: set a fixed window size for JDialog
提问by Tanay
I tried setPrefferedSizeand setSizemethods, but the dialog still opens at minimum size.
我尝试了setPreferedSize和setSize方法,但对话框仍然以最小尺寸打开。
private void method() {
commandDialog.setPreferredSize(new Dimension(100,100));
- - -
- - - //Components added to dialogPanel
commandDialog.add(dialogPanel);
// Tried this as well: commandDialog.setSize(40, 40);
commandDialog.validate();
commandDialog.setVisible(true);
}
采纳答案by Hovercraft Full Of Eels
Don't forget to call pack()
on the dialog before setVisible(true)
.
不要忘记调用pack()
之前的对话框setVisible(true)
。
Also, many here will recommend that rather than setting the dialog's preferredSize, overriding it or its content pane's getPreferredSize()
method as a cleaner and safer way of having it size itself correctly.
此外,这里的许多人会建议不要设置对话框的 preferredSize,而是覆盖它或其内容窗格的getPreferredSize()
方法,作为一种更清晰、更安全的方式来正确调整自身大小。
Edit
Holger posted:
编辑
Holger 发布:
Overriding getPreferredSize() is not better if it is just used to return the same constant size.
如果只是用于返回相同的常量大小,则覆盖 getPreferredSize() 并不是更好。
I think that we all agree that having getPreferredSize()
return a dumb constant size is usually to be avoided (although it is stable). Most of the time I avoid setting size/preferredSize or overriding getPreferredSize, but rather try to have my components and their own preferred sizes as well as the layout managers all size themselves, which again is initiated by the pack()
method. Occasionally I will need to take a more active roll in trying to set the size of a component such as if I need to size a component to a background image, and then I'll override getPreferredSize()
and use the image dimensions for the dimension returned, or will use some type of program logic to help figure out the best size. But again we all can agree that the less you futz with this, the better.
我认为我们都同意getPreferredSize()
通常要避免返回一个愚蠢的常量大小(尽管它是稳定的)。大多数时候,我避免设置 size/preferredSize 或覆盖 getPreferredSize,而是尝试让我的组件和它们自己的首选大小以及布局管理器都自己调整大小,这也是由pack()
方法启动的。有时,我需要更积极地尝试设置组件的大小,例如我需要将组件的大小调整为背景图像,然后我将覆盖getPreferredSize()
并使用返回的尺寸的图像尺寸,或者将使用某种类型的程序逻辑来帮助找出最佳尺寸。但我们再次同意,你对此越少越好。
Edit 2
Holger posted:
编辑 2
Holger 发布:
... but the question is about a JDialog and dialogs never adapt themselves to their preferred size automatically.
...但问题是关于 JDialog 和对话框永远不会自动适应他们的首选大小。
Again, they and all top-level windowsdo when you call pack()
on them. Which was why I recommend this, and why this was my primary answer to his question.
同样,当您调用它们时,它们和所有顶级窗口都会这样做pack()
。这就是我推荐这个的原因,以及为什么这是我对他的问题的主要回答。
回答by Anirban Nag 'tintinmj'
Did you try
你试过了吗
commandDialog.setSize(new Dimension(100, 100));
commandDialog.setResizable(false);
?
?