JScrollPane 的使用
我里面有一个 JScrollPane 和 JPanel(我想在 JPanel 上绘图)。我还有一种用线长度参数绘制线的方法。如果行的长度大于 JScrollPane 上 JPanel 的大小(高度或宽度),我希望 JScrollPane 滚动。 我该怎么做?
PS我尝试过 jScrollPane.scrollRectToVisible 但它不起作用
I have a JScrollPane and JPanel inside it(I want to draw on JPanel). Also I have a method that draws lines with parametre of length of the line. I want my JScrollPane to scroll if the length of the line is more then the size(height or width) of my JPanel that is on JScrollPane.
How can I do this?
P.S. I've tried jScrollPane.scrollRectToVisible but it doesn't work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个实现您想要的示例,
请注意
p.setPreferredSize(new Dimension(300, 300));
行,该行设置面板的首选大小。Here is an example implementing what you want
Note the line
p.setPreferredSize(new Dimension(300, 300));
which sets the preferred size of the panel.您需要在
LinePanel
类中定义以下方法:确保
myLine
对象可用于上述方法。并且,您将根据需要获得滚动条。PS:我假设
LinePanel
扩展JPanel
并且是绘制线条的面板。You need to define the following method in the
LinePanel
class:Make sure that
myLine
object is available to the above method. And, you will get the scrollbars as needed.P.S.: I assume
LinePanel
extendsJPanel
and is the panel on which the line is drawn.您需要将 JScrollPanel 与它正在滚动的 JPanel 关联起来。也就是说,您需要
这将创建一个 JScrollPane,它会自动调整自身大小以滚动到任何 linePanel 的大小上。
然后,重要的是要确保包含您的生产线的面板实际上足够大,可以容纳整条生产线。也就是说,您可能有类似
或任何关联的线路尺寸。请注意,类似的内容
会调整滚动面板的大小,但不会调整内部的大小 - 也就是说,它将是一个 600x400 滚动面板,但内部面板可能会更大更小,并且它将滚动。
You need to associate the JScrollPanel with the JPanel that it is scrolling. That is, you need
This will create a JScrollPane that automatically sizes itself to scroll over whatever linePanel's size is.
It's important to make sure then, that the panel containing your line actually IS big enough to fit entire line. That is, you might have something like
or whatever your associated line size is. Note that something like
will resize the scroll panel but not the thing inside--that is, it will be a 600x400 scroll pane but the inside panel may be bigger smaller, and it will be scrolled.