如何在 Java 中将 ScrollPane 添加到面板? [英] How do I add ScrollPane to a panel in Java?

查看:36
本文介绍了如何在 Java 中将 ScrollPane 添加到面板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在运行时将垂直堆叠的不同按钮添加到 JPanel 并使用 JScrollPane 以便所有按钮都可见(有一些滚动).

为了做到这一点,我将我的 JPanel 添加到 JScrollPane,然后我将按钮添加到我的 JPanel.

但是,当我这样做时,垂直滚动条不允许我看到所有图像.例如,当我添加 7 个按钮时,我只能滚动查看 5 个完整图像和 6 个图像的一半.

为什么我的滚动条不允许我显示所有 7 个按钮?

解决方案

像这样创建面板和滚动面板:

JPanel panel = new JPanel();JScrollPane scrollPane = new JScrollPane( panel );

在运行时向面板添加按钮时,代码应为:

panel.add( button );panel.revalidate();

只要您使用布局管理器,就会重新计算首选大小并显示滚动条.

I want to add different buttons, vertically stacked, to a JPanel at run-time and use a JScrollPane so that all buttons will be visible (with some scrolling).

In order to do this, I have added my JPanel to a JScrollPane, after which I add buttons to my JPanel.

However, when I do this the vertical scrollbar does not allow me to see all images. For example when I add 7 buttons I can only scroll to see 5 full images and half of the 6 images.

Why doesn't my scrollbar allow me to display all 7 buttons?

解决方案

Create the panel and scrollpane like:

JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane( panel );

When you add buttons to the panel at run time the code should be:

panel.add( button );
panel.revalidate();

As long as you are using a layout manager the preferred size will be recalculated and the scrollbar will appear.

这篇关于如何在 Java 中将 ScrollPane 添加到面板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆