FlowLayout 不与 JScrollPane 一起流动

发布于 2024-11-02 17:15:43 字数 876 浏览 3 评论 0原文

我在 JPanel 上使用 FlowLayout 有一堆按钮。看起来真的很好看。当按钮到达面板的右侧时,它们会在新行上开始创建一个漂亮的二维网格。

这是代码:

    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);

按钮仅显示在一行中(我什至看不到右侧的按钮)。我还是想让他们包起来。这是怎么回事?顺便说一句,我只需要显示垂直滚动条,而不显示水平滚动条。我希望按钮在到达右端时换行到新行。

I have a bunch of buttons on a JPanel using a FlowLayout. It looks really nice. When the buttons reach the right side of the panel they start out on a new row creating a nice 2-dimensional grid.

Here is the code:

    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);

However, the minute I put the panel in a scroll pane with only vertical scrolling:

    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);

the buttons are only shown in one row (and I can't even see the ones off to the right). I still want them to wrap. What is going on here? By the way, I need to show only the vertical scroll bar and no horizontal scroll bar. I want the buttons to wrap to a new row when they reach the right end.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谜兔 2024-11-09 17:15:43

在这里查看我的答案:如何让 JToolBars 换行到下一行 (FlowLayout),而不隐藏它们下面的 JPanel?

在这种情况下,我使用了一个自定义的流布局。只需将其插入,它甚至会包裹在滚动窗格中。

Check out my answer here: How can I let JToolBars wrap to the next line (FlowLayout) without them being hidden ty the JPanel below them?

There is a custom Flow Layout that I have used in this exact situation. Just plug it in and it wraps even in a scroll pane.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文