问题描述
所以我有一堆JTable
.每个JTable
都在一个JScrollPane
内.然后我将这些 JScrollPane
中的每一个添加到 JPanel
.然后我将这个 JPanel
添加到一个 JScrollPane
然后添加到另一个 JPanel
和 BorderLayout
.较大的 JScrollPane
与其父级一起正确调整大小,但每个较小的 JScrollPane
都有恒定的高度,当窗口很小时,它比窗口大.我怎样才能让每个子 JScrollPane
s 随窗口/它们的父级的高度调整大小?
So I have a bunch of JTable
s. Each JTable
is inside a JScrollPane
. I'm then adding each of these JScrollPane
s to a JPanel
. I'm then adding this JPanel
to a JScrollPane
then that to another JPanel
with BorderLayout
. The larger JScrollPane
properly resizes with its parent, but each of the smaller JScrollPane
s have constant height, which is larger than the window when it is small. How can I get each of the children JScrollPane
s to resize with the height of the window/their parent?
我尝试使用 FlowLayout
、BorderLayout
添加更多中间 JPanel
,但似乎没有任何效果.
I've tried adding more intermediary JPanel
s with FlowLayout
, BorderLayout
, and nothing seems to work.
这是一些相关的代码:
public class MyPanel extends JPanel
{
public MyPanel()
{
super(new BorderLayout());
JPanel panel = new JPanel();
for (int i = 0; i < 5; i++)
{
// View extends JTable
panel.add(new JScrollPane(new View(new Model())));
}
add(new JScrollPane(panel));
}
}
我真的只是想在一个带有水平滚动条的较大面板内获得一堆带有水平滚动条的表格.当窗口大小发生变化时,我希望所有这些东西都能适当地调整大小.
I'm really just trying to get a bunch of tables with scrollbars horizontally next to each other inside a larger panel with a horizontal scrollbar. And I want all that stuff to resize appropriately when the window size changes.
更多代码:
final MyPanel panel = new MyPanel();
final JTabbedPane tabView = new JTabbedPane();
tabView.add(...);
tabView.add("foo", panel);
final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, ..., tabView);
this.add(splitPane); // this extends JFrame
推荐答案
您可以使用 BoxLayout.如果你想要相反的:一些表被固定,你可以用约束包裹它 Box.createRigidArea(new Dimension(100, 100)) .
You can use a BoxLayout. If you want the opposite: some table being fixed, you can wrap it with constraint Box.createRigidArea(new Dimension(100, 100)) .
这篇关于如何在 JScrollPane 中获取 JScrollPanes 以跟随父级的大小调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!