Java 的 setPreferredSize 不会调整 JPanel 的大小

发布于 2024-08-03 22:41:36 字数 484 浏览 4 评论 0原文

我正在努力(!)java GUI 开发。所有这些看似有意义的小事却行不通。我将解释我正在尝试做什么以及我失败的地方(这非常令人沮丧):

我有一个位于面板内的graphics2d 对象。现在这个图形绘制了一个图像,它可以继续绘制下去。这张图放大得很可怕,因为我不知道需要多大的尺寸,所以我把它放在滚动窗格中。现在我想在每次痛苦迭代中调整 JPanel 的大小,并将 setPreferredSize 设置为更大的值,从而扩展滚动窗格的旋钮。

问题: 由于某种原因,当我从 Paint 中调用 setPreferredSize 时,它​​没有执行任何操作。我在面板上调用它。当我尝试从调用 Paint 的函数中调用 setPreferredSize 时,它​​仅执行一次调整大小操作!

原因可能是什么?

除此之外,我还有另一个小问题,我很想解决: 由于图形绘制会继续进行并且尺寸会随着时间的推移而增加,因此如何提前滚动窗格的位置?逐渐增加吗?

人们,非常感谢你们。我确实需要帮助

I am struggling ( ! ) with java GUI development. All these small things that make obvious sense just don't work. I'll explain what I am trying to do and where I am failing (this is quite frustrating):

I've got this graphics2d object which resides inside a panel. Now this graphics draws an image, it can go on and draw forever. this drawing expands horrioncally and since I do not know what size to expect I put it inside a scrollpane. Now I want to resize the JPanel in each pain iteration and set the setPreferredSize to a larger value, thus expanding the scrollpane's knob.

The problem:
From some reason when I call setPreferredSize from paint it doesn't do anything. I call it on the panel. When I try calling setPreferredSize from the function that calls paint, it performs the resizing operation only once!

What could be the reason for that?

Apart from that I've got another small question, I'd love to solve:
Since the graphics draw goes on and increases in size in time, how can I advance the position of scrollpanes? Gradually increasing it?

People, thank you very much. I sure need help here

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

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

发布评论

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

评论(4

浅浅淡淡 2024-08-10 22:41:36

顾名思义,首选大小不是强制性的...也许 pack() 或 revalidate() 会有所帮助...

preffered size is, by its name, not mandatory... maybe pack() or revalidate() would help...

つ低調成傷 2024-08-10 22:41:36

在我看来,你好像做错了几件事。

  • 大小属性(实际上是组件的任何属性)都可能会导致奇怪的行为,因为您并不真正知道调用 Paint 的时间和频率

  • 您通常不会调用自己的 Paint。一旦组件变得可见,它就会被 Swing 本身调用

  • 如果我没猜错的话,你想要一个随时间增长的组件。恕我直言,正确的方法是创建一个计时器。使用计时器根据需要更改大小。在绘制方法中,只需读取此信息并使用它来决定绘制什么。

Sounds to me like you got a couple of things wrong.

  • Setting any size attribute (actuallly any attribute of a component at all) inside the paint method will result in possibly strange behavior, because you don't really know when and how often paint gets called

  • You normally do not call paint your self. It will get called by Swing itself, once the component becomes visible

  • If I got it right you want to have a Component, that grows with time. IMHO the correct approach would be to create a Timer. Using the Timer change the size as required. In the paint method just read this information and use it to decide what to paint.

夏夜暖风 2024-08-10 22:41:36

setPreferredSize 是您可以向 LayoutManager 提供的众多提示之一,如果确实已设置 LayoutManager,则该 LayoutManager 正在管理组件容器的布局。这取决于给定的 LayoutManager 对这些提示的处理方式。

我建议您查看Filthy Rich Clients 这本书,深入了解 Swing 组件渲染的工作原理。

setPreferredSize is one of many hints you can give to the LayoutManager which is managing the layout of your component's container if indeed a LayoutManager has been set. It is up to the given LayoutManager what it does with those hints.

I suggest you check out the book Filthy Rich Clients for an in-depth explanation of how the Swing component rendering works.

笑咖 2024-08-10 22:41:36

您应该注意 set(Preferred/Minimal/Maximum)Size() 只是提示父组件及其布局管理器组件应获得多少空间。
进一步注意,这些大小仅在组件布局时(首次渲染、invalidate() 等)才会被解释。

You should note that set(Preferred/Minimal/Maximum)Size() are nothing more than hints to the parent component and its layoutmanager how much space the component should get.
Further note that theses sizes are only interpreted when the components are layouted (upon first render, invalidate() and so on).

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