带有flowlayout的java scrollpane [英] java scrollpane with flowlayout

查看:142
本文介绍了带有flowlayout的java scrollpane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,我不能这样做。它应该很简单。

For some reason, I cannot this working. It should be simple really.

我有一个带有 BorderLayout 的JFrame,其中包含 JPanel (南)和(CENTER)JPanel(itemPanel)。

I've having a JFrame with a BorderLayout, which contains a JPanel (SOUTH) and a (CENTER) JPanel (itemPanel).

itemPanel应该包含在滚动窗格中。它的width = x,并且它的所有子节点共享它的长度,所以它基本上就像一个只有1列的表。

itemPanel should be wrapped in a scrollpane. Its width = x, and all of its children shares its length, so it basically works like a table with only 1 column.

出于某种原因,我无法获得滚动窗格来显示滚动条(和滚动)。在JFrame中:

For some reason, I cannot get the scrollpane to show the scrollbars (and scroll). In the JFrame:

setPreferredSize(dimension);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().add("South",controlPanel);
JScrollPane scroll = new JScrollPane(itemPanel);
scroll.setBorder(null);
getContentPane().add("Center",scroll);
super.pack();
setVisible(true);

启动并添加一些虚拟面板到itemPanel:

Initilizing and adding some dummy-panels to the itemPanel:

itemPanel = new ItemPanel(); // A JPanel with a flowlayout
itemPanel.setPreferredSize(new Dimension(dimension.width,0));
for(int i = 0; i < 20; i++){
  JPanel p = new JPanel();
  p.setPreferredSize(new Dimension(0,50));
  p.setBackground(i%2 == 0 ? Color.GREEN : Color.YELLOW);
  itemPanel.add(p);
}

如果省略 itemPanel.setPreferredSize(new Dimension(dimension) .width,0)); 滚动窗格显示水平滚动条,但由于flowlayout没有要跟随的宽度,它只显示一行中的组件。

if omitting itemPanel.setPreferredSize(new Dimension(dimension.width,0)); the scrollpane shows the horizontal scrollbars, but since the flowlayout does not have a width to follow, it just shows the components in one row.

推荐答案

WrapLayout 可能就是你要找的东西。

WrapLayout may be what you are looking for.

这篇关于带有flowlayout的java scrollpane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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