根据JPanel图像内容调整JFrame的大小?

发布于 2024-10-01 05:00:18 字数 624 浏览 1 评论 0原文

我正在使用 NetBeans 开发一个简单的图像浏览器。我有类 ImageBrowser ,它扩展了 JFrame (通过右键单击 NetBeans 项目,然后单击“New -> JFrame Form”创建它),其中有一个 JPanel code> 和一个 JToolbar,其中 JButtons 从用户选择的目录中生成为图像缩略图。

单击图像缩略图会在此主 JPanel 上显示整个图像。基本上,图像的大小不同,我希望 JFrame(即整个窗口)自动调整到图像的大小。

我尝试在每次 JPanel 内容更改时(即当用户单击缩略图并触发 ActionEvent 时)为主“JFrame”调用 .pack() ,但它给了我以下错误:

静态内容无法引用非静态方法 pack()

所以我相当坚持这一点,我想知道是否有办法做到这一点。如果有,是否也可以设置 JFrame 窗口大小的最大限制?

至于布局,JFrame 是一种自由布局,使用 NetBeans GUI 开发人员构建。

I'm using NetBeans to develop a simple image browser. I have the class ImageBrowser which extends JFrame (created it by right clicking the NetBeans project then New -> JFrame Form) and inside it is a JPanel and a JToolbar with JButtons generated as image thumbnails from a directory the user selects.

Clicking an image thumbnail displays the image as a whole on this main JPanel. Basically the images are of different sizes and I want the JFrame (ie. entire window) to automatically adjust to the size of the image.

I've tried calling .pack() for the main `JFrame' every time the JPanel content changes (ie. when the user clicks a thumbnail and fires an ActionEvent), but it's giving me this error:

non-static method pack() cannot be referenced from static content

So I'm rather stuck on this and I'm wondering if there's a way to do it. If there is, is it also possible to set a maximum limit on the JFrame window size?

As for layout, the JFrame is a free layout, constructed using the NetBeans GUI developer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执妄 2024-10-08 05:00:18

静态内容无法引用非静态方法 pack()

不要在应用程序中使用静态方法。没有必要这样做。唯一的静态方法应该是 main() 方法和“createAndShowGUI”方法(假设您遵循 Swing 教程示例

您可以使用从 ActionEvent 获取源代码来查找要打包的窗口

SwingUtilities.windowForComponent(...);

,然后使用该组件查找要打包的窗口。

是否也可以设置 JFrame 窗口大小的最大限制?

进行打包()。然后,如果尺寸超出限制,您始终可以通过在窗口上调用 setSize() 来覆盖最大尺寸。

我也同意,每次图像更改时窗口的大小都会不断变化,这不是一个好的设计。

我会将图像添加到 JLabel 并将标签添加到滚动窗格,以便当图像大于框架大小时可以出现滚动条。

non-static method pack() cannot be referenced from static content

Don't use static methods in your application. There is no need to do this. The only static method should be the main() method and the "createAndShowGUI" method assuming you follow the example code found in the Swing tutorial examples.

You can find the Window to pack() by using

SwingUtilities.windowForComponent(...);

You get the source from the ActionEvent and then use that component to find the Window to pack.

is it also possible to set a maximum limit on the JFrame window size?

Do the pack(). Then you can always overide the maximum size by invoking setSize() on the Window if the dimensions exceed you limit.

I also agree, it is not a good design to continually have the size of the Window change each time the image is changed.

I would add the Image to a JLabel and add the label to a scrollpane to that scrollbar can appear when the image is greater than the size of the frame.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文