java FlowLayout没有围绕JScrollPane流动
我在使用FlowLayout的JPanel上有一堆按钮。看起来真不错。当按钮到达面板的右侧时,它们开始在一个新行上创建一个漂亮的二维网格
代码如下:
Container cp = getContentPane();
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
for (int i = 0; i < 20; i++)
panel.add(new JButton("Button " + i));
cp.add(panel);
但是,当我将面板放在滚动窗格中时,只有垂直滚动:
Container cp = getContentPane();
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
for (int i = 0; i < 20; i++)
panel.add(new JButton("Button " + i));
JScrollPane pane = new JScrollPane(
panel,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
cp.add(pane);
按钮只显示在一行中(我甚至看不到右边的按钮)。我还是想把它们包起来。这是怎么回事?顺便说一下,我只需要显示垂直滚动条,没有水平滚动条。我希望按钮到达右端时换行
# 1 楼答案
在这里查看我的答案:How can I let JToolBars wrap to the next line (FlowLayout) without them being hidden ty the JPanel below them?
在这种情况下,我使用了一个自定义的流布局。只需插入它,它甚至可以包装在滚动窗格中