Java FileDialog 以编程方式设置大小
我想显示一个 Java FileDialog 并将其初始大小设置为合理的值(它不显示最大化按钮,很多用户可能不知道可以使用 alt-space X 代替)。 我尝试了 setBounds 但它没有做任何事情,而且我没有看到最大化它的方法; 我缺少什么?
更新:至少在 Windows 上,系统文件对话框即使在程序调用之间也会记住大小和最大化状态,因此用户只需将其设置为所需的大小一次,并且它将保持这种状态; 这似乎足够了,所以我就到此为止。
I want to display a Java FileDialog and sets its initial size to something reasonable (it doesn't show the maximize button, and a lot of users might not know you can use alt-space X instead). I tried setBounds but it didn't do anything, and I don't see a method for maximizing it; what am I missing?
Update: on Windows at least, the system file dialog remembers size and maximize state even across program invocations, so the user can set it to the desired size just once and it will stay that way; that seems sufficient, so I will leave it at that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要调用 pack(),然后调用 setSize()。 直到包装完毕,才意识到。 在 setVisible() 之前进行打包。 顺便说一句, pack() 将调整对话框的大小,以便内容获得首选大小,这可能就是您想要的。
我使用 AWT FileDialog,并且在大小方面没有任何问题(我在尝试正确定位它时遇到了问题,但这是一个单独的问题)。 我的代码很明显:
根据我的经验,默认大小是完全合理的。 当然,它可以用鼠标调整大小 - 因为它是一个对话框,而不是一个框架,所以它不能最大化。
我只是在 dlg.setVisible() 之后尝试了 setSize() 的一些变体,它确实完全忽略了它们,也许是因为它是一个重量级窗口,对于它的大小和位置有自己的想法。 我必须承认,我一直对 FilenameFilter 在 Windows 中不起作用感到惊讶,这听起来像是一个奇怪的限制。
You need to call pack() and then setSize(). Until it's packed, it's not realized. Do the pack before setVisible(). BTW, pack() will size the dialog so that the contents get their preferred size, so that might be what you want.
I use an AWT FileDialog, and have had no problems with size (I have had problems with trying to get it to be located properly, but that's a separate issue). My code is just the obvious:
The default size is perfectly reasonable in my experience. And of course it's resizable with the mouse - since it's a dialog, and not a frame it's not maximizable.
I just tried a few variations on setSize() after my dlg.setVisible(), and it does completely ignore them, perhaps because it's a heavyweight window with it's own ideas about it's size and location. I must admit I have always been surprised that the FilenameFilter doesn't function in Windows, which sounds like an odd limitation.
首先,我建议您使用 swing 中的 JFileChooser (除非由于某种原因您被限制使用 awt)。 它有更多的选择。
其次,它们都继承自具有 setsize 方法的 Component。 您可以获取用户屏幕的尺寸并将尺寸设置为该尺寸的百分比。
您绝对想最大化它吗? 如果是这样,只需将尺寸设置为用户屏幕的尺寸即可。
您可以通过以下方式获取屏幕尺寸: Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
如果这不能回答您的问题,请更准确。
First of all, I'd recommend you use JFileChooser from swing (unless you are restricted to awt for some reason). It got a lot more options.
Second, they both inherit from Component which has a setsize method. You can get the size of the user's screen and set the size to a percentage of that.
Do you absolutly want to maximise it ? If so just set the size to the size of the user's screen.
You can get the screen size with : Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
If this doesn't answer your question, please be more precise.