Java Swing setMaximumSize 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10157954/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:51:21  来源:igfitidea点击:

Java swing setMaximumSize not working

javaswingjframe

提问by Matthieu Riegler

Possible Duplicate:
setMaximumSize not working in java

可能的重复:
setMaximumSize 在 java 中不起作用

I've having trouble with my JFramesubclass. I need to set a Maximum height.

我的JFrame子类有问题。我需要设置一个最大高度。

But setMaximumSizedoes not work. It seems that's a bug in java (setMinimumSize works). How could prevent a JFramefrom getting higher than a given height ?

setMaximumSize不起作用。这似乎是 java 中的一个错误(setMinimumSize 有效)。如何防止 aJFrame高于给定的高度?

采纳答案by Ozzy

Use this instead/as well:

使用这个代替/以及:

setPreferredSize(new Dimension(width, height));

In my experience setPreferredSize(Dimension)takes preference and always works.

根据我的经验setPreferredSize(Dimension),优先考虑并且总是有效。

I usually use all of them anyway: setMinimumSize(), setMaximumSize(), setPreferredSize()and then setSize()as well

无论如何setMinimumSize(),我通常都会使用它们:, setMaximumSize(),setPreferredSize()然后setSize()也是

回答by ControlAltDel

In my experience, setMinimumSize and setMaximumSize are not reliable - they may work in some situations, but often not in others

根据我的经验, setMinimumSize 和 setMaximumSize 并不可靠 - 它们可能在某些情况下有效,但在其他情况下通常无效

The best solution is to use a ComponentListener, implement componentResized, and enforce the min / max size that way

最好的解决方案是使用 ComponentListener,实现 componentResized,并以这种方式强制执行最小/最大大小