FlowLayout不随其周围的JScrollPane流动 [英] FlowLayout not flowing with JScrollPane around it

查看:140
本文介绍了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);

按钮仅显示在一行中(我什至看不到右边的按钮).我还是要他们包好.这里发生了什么?顺便说一句,我只需要显示垂直滚动条而不显示水平滚动条.我希望按钮到达右端时换行到新行.

解决方案

在此处查看我的答案:

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.

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.

这篇关于FlowLayout不随其周围的JScrollPane流动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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