JScrollpane 滚动条调整大小问题
我有以下问题:
我有包含图像的 JScrollPane。
滚动窗格与其他组件一起位于 JPanel 内部,以便能够使用 BorderLayout 布局整个内容(滚动窗格位于中心,我在南部有一些东西)。
该 JPanel 位于 JSplitPane 中。
JSplitPane 最终位于 JFrame 内。
JFrame
{
JSplitpane
{
JPanel(BorderLayout)
{
JScrollPane(CENTER)
{
BufferedImage
}
JPanel(SOUTH)
{...}
}
}
}
我现在的问题是,当我调整框架大小时,scrollPane 中的图像会很好地调整大小,直到达到图像大小。然后图像被拉伸,这是我想防止的。 我尝试将 JScrollPane 包装在使用 FlowLayout 进行布局的 JPanel 中。这对于调整大小来说效果很好,但是一旦我调整框架大小,滚动条就会消失。
我重写了scrollPane的setSize()方法,发现一旦调整框架大小,滚动窗格的大小总是设置为图像的大小。但我不知道如何解决这个问题。
每一个提示都值得赞赏。
干杯,
本
I have the following problem:
I have JScrollPane containing an image.
The scrollpane is inside a JPanel together with other components in order to be able to layout the whole thing with a BorderLayout (the scrollpane is the CENTER and I have something in the SOUTH).
This JPanel is in a JSplitPane.
The JSplitPane is finally inside a JFrame.
JFrame
{
JSplitpane
{
JPanel(BorderLayout)
{
JScrollPane(CENTER)
{
BufferedImage
}
JPanel(SOUTH)
{...}
}
}
}
My problem now is that when I resize my frame the image in the scrollPane resizes fine until it reaches the images size. Then the image is stretched which I want to prevent.
I tried wrapping the JScrollPane in a JPanel that is layout with a FlowLayout. That works fine for the resizing bit but the scrollbars disappear once I resize the frame.
I overrode the scrollPane's setSize() method and found out that the size of the scrollpane is always set to the image's size once the frame is resized. I don't have a clue how to fix that though.
Every hint is appreciated.
Cheers,
Ben
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信如果您使用
BufferedImage
的宽度/高度在JScrollPane
上调用setMaximumSize()
,您应该会很好(但还没有尝试过)它)。作为一般性评论:仅仅为了添加子组件而重写 Swing 组件是一个坏主意。相反,创建一个控制器类来创建和组合基本组件。
我重读了你的问题,想知道:问题是图像被拉伸,还是滚动条消失了?如果您想在没有任何内容可滚动的情况下将滚动条保留在屏幕上,请查看
setVerticalScrollBarPolicy()
和setHorizontalScrollBarPolicy()
方法。I believe you should be good if you call
setMaximumSize()
on theJScrollPane
with the width/height of theBufferedImage
(but haven't tried it).And as a general comment: it's a bad idea to override Swing components just to add children. Instead, create a controller class that creates and composes base components.
I reread your question, and am wondering: is the problem that the image gets stretched, or that the scrollbars are disappearing? If you want to keep the scrollbars on the screen even when there's nothing to scroll, take a look at the
setVerticalScrollBarPolicy()
andsetHorizontalScrollBarPolicy()
methods.