JFrame 仅可调整高度
JFrame.setResizable(true)
允许用户调整窗口的宽度和高度。是否存在允许用户仅调整高度大小的方法?
谢谢。
编辑:以下解决方案似乎不起作用。在 360x600 JFrame 上,
setResizable(true);
pack();
setMaximizedBounds(new java.awt.Rectangle(0, 0, 360, 1200));
setMaximumSize(new java.awt.Dimension(360, 1200));
setMinimumSize(new java.awt.Dimension(360, 600));
setPreferredSize(new java.awt.Dimension(360, 600));
setVisible(true);
Still 允许完全拉伸 JFrame 的宽度,并且设置 setResizes(false)
不允许拉伸任何内容。
JFrame.setResizable(true)
lets the user resize both the width and height of a window. Does a method exist which allows the user to ONLY resize the height?
Thanks.
Edit: The solutions below do NOT seem to work. On a 360x600 JFrame,
setResizable(true);
pack();
setMaximizedBounds(new java.awt.Rectangle(0, 0, 360, 1200));
setMaximumSize(new java.awt.Dimension(360, 1200));
setMinimumSize(new java.awt.Dimension(360, 600));
setPreferredSize(new java.awt.Dimension(360, 600));
setVisible(true);
Still allows fully stretching the width of the JFrame, and setting setResizable(false)
allows nothing to be stretched.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
下面的代码可以正确完成工作。
The code below does the job right.
如果您有 JNI 经验,那么这是一个可能的解决方案。
在 Windows 和 Oracle Hotspot 中,此代码将允许在最小和最大尺寸之间调整窗口大小,而不会闪烁或导致任何令人讨厌的 JFrame 调整大小后副作用。如果在创建 JFrame 并使其可见后调用此代码,则可以完全取消上面在 componentResized() 中用于捕获调整大小事件的代码,因为 Windows 具有防止用户能够调整上方或下方大小的功能指定的最小/最大尺寸。
// Java 代码:
// C++ 代码(包括标准 windows.h、winbase.h 等)
// 以下是准确获取 HWND 的代码:
// Java 代码(添加到上面的 Java 代码
中) // C++ 代码
If you have experience with JNI, then this is a possible solution.
In Windows and Oracle Hotspot, this code will allow a window to be resized between minimum and maximum dimensions without flickering or causing any of the nasty JFrame-post-resize side-effects. If this code is called once the JFrame is created and made visible, then the code used above in componentResized() to catch resize events can be completely done away with, as Windows has facilities to keep the user from being able to resize above or below the specified min/max sizes.
// Java code:
// C++ code (include standard windows.h, winbase.h, etc)
// The following is code to get the HWND accurately:
// Java Code (add to Java code above)
// Code in C++
我认为没有一种方法明确用于此目的。但是,您可以预设 JFrame 的首选尺寸、最小尺寸和最大尺寸,以使宽度全部相等。
您可能希望在
frame.pack()
之后和frame.setVisible(true)
之前执行此操作。I do not think there is a method expressly for that purpose. However, you could preset the JFrame's preferred, minimum, and maximum size such that the widths are all equal.
You will probably want to do this after
frame.pack()
and beforeframe.setVisible(true)
.我相信大多数平台都会尊重
setMaximumSize
和setMinimumSize
。还有setMaximizedBounds
。不起作用的是添加一个侦听器来重置宽度。线程问题使它看起来不愉快。如果您有 PL&F 装饰的窗口(Windows PL&F 不支持),那么它们可能会被黑客攻击。
I believe most platforms will honour
setMaximumSize
andsetMinimumSize
. There is alsosetMaximizedBounds
.What doesn't work is adding a listener to reset the width. Threading issues makes it look unpleasant. If you have PL&F decorated windows (not supported by the Windows PL&F), then they can be hacked about with.
使用
setResizing(false)
而不是setResizable(true)
事实上 setResizable(false) 为我解决了这个问题。 它甚至禁用了最大化(使图标变暗)
Instead of
setResizable(true)
usesetResizable(false)
Infact setResizable(false) did the trick for me. It even disabled maximization (dimmed the icon)
为了避免闪烁,您可以重写 JFrame 中的 #setBounds 方法。似乎每次调整大小都会调用它。类似的东西
对我有用。
To avoid flickering, you can override #setBounds method in your JFrame. Seems like every resize will call it. Something like
did the trick for me.
我正是出于此目的使用此代码并且它有效(它实际上是编辑的@Hoazhun解决方案,因为他的原始代码将锁定宽度和高度,但在我的版本中,您可以手动设置高度,但具有宽度固定的):
I am using this code exactly for this purpose and it works (it is actually edited @Hoazhun solution as his original code will lock both width and height, but in my version you can set height manually yet having width fixed):