带有 JPanel 可滚动的文件浏览器
我正在尝试在面板中显示一堆图像。该面板将具有固定大小,并且可以滚动以显示所有图片,这是一种缩略图库。事实上,我不明白 JScrollPanel
如何与 JPanel
等一起工作。有人可以帮助获得像 Windows 资源管理器一样可滚动的 JPanel
吗?包含组件且可滚动的面板。
我有一个扩展 JPanel 的 FlowLayoutPanel ……
public FlowLayoutPanel ()
{
setLayout(new FlowLayout());
this.setSize(new Dimension(1000,1000));
}
现在
,我可以向该面板添加图像了!但是,当 JPanel 达到最大(最大尺寸)时,我希望在右侧看到一个漂亮的滚动条,以便滚动浏览我的面板!
I'm trying to show a bunch of images in a panel. That panel would have a fix-size and will be scrollable to display all the pictures, a kind of thumbnail gallery. In fact, I don't understand how the JScrollPanel
work with a JPanel
and others. Could someone help to get a JPanel
scrollable like a Windows Explorer? A panel containing components and scrollable.
I have a FlowLayoutPanel which extends JPanel
...
public FlowLayoutPanel ()
{
setLayout(new FlowLayout());
this.setSize(new Dimension(1000,1000));
}
...
Now, I can add images to that panel ! But when the JPanel gets filled at is max (Maximum size), I would like to see a beautiful scrollbar on the right to just scroll through my Panel !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您可以使用 JList。当
水平换行
开启时,它支持自动换行。那么您需要一个自定义布局管理器,而不是自定义面板。请参阅换行布局。
Maybe you can use a JList. It supports automatically wrapping when
horizontal wrap
is turned on.Then you need a custom layout manager, not a custom panel. See Wrap Layout.
首先也是最重要的——不要设置 JPanel 的大小。让布局管理器决定尺寸应该是多少。您需要做的是创建一个新的 JScrollPane 并将该面板传递到 JScrollPane 的构造函数中。然后,您需要设置 JScrollPane 或其视口的
preferredSize
(因为大多数布局管理器都遵循 PreferredSize,而不是大小)。First and foremost -- Don't set the size of that JPanel. Let the layout managers decide on what the size should be. What you'll want to do is to create a new JScrollPane and pass this panel into the JScrollPane's constructor. Then you'll want to set the
preferredSize
of the JScrollPane or its viewport (as most layout managers respect the preferredSize, not the size).