Java 1.5 中 JScrollPane 中 JPanel 的大小不正确

发布于 2024-08-30 17:14:25 字数 867 浏览 4 评论 0原文

我正在制作一个图像加载组件,它由一个包含 JScrollPane 的 JPanel 组成,而 JScrollPane 又包含另一个 JPanel。该组件的作用是允许将图像放置在其顶部,然后加载图像并将最内部的 JPanel 设置为放置的图像的大小。这反过来会导致滚动条显示,用户可以滚动图像。这一切都很好。当我尝试将图像自动缩小到外部 JPanel 中的最大可见区域时,问题就出现了。在这种情况下,我对图像进行统一缩放,使其小于或等于外部 JPanel 的宽度和高度。现在发生的情况是,水平和垂直滚动条都显示,表明内部 JPanel 大于可见区域(不应该是这种情况)。我验证了图像是否缩放到正确的尺寸(即尊重最大宽度和高度)。我还验证了如果我将最大高度减少 3 个像素,则不会出现滚动条。

我认为问题是, panel.getWidth() 和 panel.getHeight() 实际上并没有返回子组件可以占用的可见区域(最大区域)。 IE。 JPanel 周围的边框或类似的东西可能会占用更多的宽度和高度。

我的问题是,我该如何解决这个问题。从功能上讲,我想要的只是确定 JPanel 在 JScrollPane 中的最大大小,然后将面板设置为该大小并在其顶部绘制图像,并确保滚动窗格的滚动条不会显示。现在滚动条设置为 AS_NEEDED。

这是部分代码:

JPanel. Insets insets = getInsets();
int width = getWidth() - insets.left - insets.right;
int height = getHeight() - insets.top - insets.bottom;
imageScaled = ImageUtils.uniformScaleImage(imageOriginal, width, height);

谢谢!

I am making an image loading component which consists of a JPanel containing a JScrollPane, which in turn contains another JPanel. What this component does is allows images to be dropped on top of it, after which point the image is loaded and the inner most JPanel is set to the size of the image dropped. This in turn causes the scroll bars to show up and the user can scroll the image. This all works fine. The problem comes in when i try to auto-shrink the image to the maximum visible area in the outer JPanel. In this case i do a uniform scale of the image to be less than or equal to the width and height of the outer JPanel. What happens now is that both the horizontal and vertical scroll bars show up indicating the the inner JPanel is bigger than the visible area (which should not be the case). I verified that the image is scale to the proper dimensions(ie. the maximum width and height is respected). I also verified that if i decrease the maximum height by 3 pixels, then no scroll bars appear.

What i believe the problem is, is that panel.getWidth() and panel.getHeight() don't actually return the visible area (maximum area) that sub components can take up. Ie. there is likely some more width and height taken up by the border around the JPanel or something like that.

My question is, how do i get around this problem. Functionally all i want is to determine the maximum size a JPanel can be in a JScrollPane, then set the panel to that size and paint an image over top of it and be assured that the scroll bars of the scroll pane will not show up. Right now the scroll bars are set to AS_NEEDED.

Here's a portion of the code:

JPanel. Insets insets = getInsets();
int width = getWidth() - insets.left - insets.right;
int height = getHeight() - insets.top - insets.bottom;
imageScaled = ImageUtils.uniformScaleImage(imageOriginal, width, height);

Thanks!

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

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

发布评论

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

评论(1

別甾虛僞 2024-09-06 17:14:25

从功能上来说,我想要的只是
确定 JPanel 的最大尺寸
可以在 JScrollPane 中

scrollPane.getViewport().getViewSize();

Functionally all i want is to
determine the maximum size a JPanel
can be in a JScrollPane

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