【问题标题】:JPanel inside a JScrollPaneJScrollPane 中的 JPanel
【发布时间】:2011-05-11 01:42:00
【问题描述】:

我有一个 JPanel,我在里面动态创建 JCheckBoxes。 这些必须始终并排添加 JCheckBoxes。如果侧面有更多空间要插入,则会创建一个新的 JCheckBoxes 行,就像在简单的文本编辑器中一样。

这正在完美地发生。但是……

我将这个 JPanel 上的布局设置为 FlowLayout,正是我想要的。

明显的问题是窗口的空间有限。所以一个很好的解决方案是:在 JScrollPane,l 中插入这个 JPanel 并使其仅在垂直滚动中发生。 但我有问题。尽管您只能显示一个垂直滚动条,但这些项目总是“永远”并排添加的。而且垂直滚动根本不起作用,只能水平滚动。

我尝试了很多方法来仅垂直滚动,但没有任何效果(如果有效,我就不会在这里:])。

那么,有没有人遇到过类似的问题,可以帮帮我吗?

我会非常感谢那些帮助我的人。

没有了。

【问题讨论】:

  • 这有点糟糕的 UI 设计 - JScrollPane 通常用于提供表格或图像的视图,而不是控件面板(毕竟,您在 Windows、MacO 中看到过多少次这种方法, 等等)?更好的方法是使用 CardLayout 将控件分离到多个选项卡或 JPanel 堆叠。

标签: java swing jpanel jscrollpane vertical-scrolling


【解决方案1】:

我用 ScrollPanes 和 FlowLayouts 处理了同样的问题。我发现最好的解决方案是使用考虑到垂直变化的 FlowLayout 的修改版本。这是这种布局的代码。您可以将它包含在您的项目中并像 FlowLayout 一样调用它,但它实际上可以很好地与滚动窗格一起使用。

import java.awt.*;

/**
  * A modified version of FlowLayout that allows containers using this
  * Layout to behave in a reasonable manner when placed inside a
  * JScrollPane

  * @author Babu Kalakrishnan
  * Modifications by greearb and jzd
  */

 public class ModifiedFlowLayout extends FlowLayout {
       public ModifiedFlowLayout() {
              super();
           }

           public ModifiedFlowLayout(int align) {
              super(align);
           }
       public ModifiedFlowLayout(int align, int hgap, int vgap) {
          super(align, hgap, vgap);
       }

       public Dimension minimumLayoutSize(Container target) {
          // Size of largest component, so we can resize it in
          // either direction with something like a split-pane.
          return computeMinSize(target);
       }

       public Dimension preferredLayoutSize(Container target) {
          return computeSize(target);
       }

       private Dimension computeSize(Container target) {
          synchronized (target.getTreeLock()) {
             int hgap = getHgap();
             int vgap = getVgap();
             int w = target.getWidth();

             // Let this behave like a regular FlowLayout (single row)
             // if the container hasn't been assigned any size yet
             if (w == 0) {
                w = Integer.MAX_VALUE;
             }

             Insets insets = target.getInsets();
             if (insets == null){
                insets = new Insets(0, 0, 0, 0);
             }
             int reqdWidth = 0;

             int maxwidth = w - (insets.left + insets.right + hgap * 2);
             int n = target.getComponentCount();
             int x = 0;
             int y = insets.top + vgap; // FlowLayout starts by adding vgap, so do that here too.
             int rowHeight = 0;

             for (int i = 0; i < n; i++) {
                Component c = target.getComponent(i);
                if (c.isVisible()) {
                   Dimension d = c.getPreferredSize();
                   if ((x == 0) || ((x + d.width) <= maxwidth)) {
                      // fits in current row.
                      if (x > 0) {
                         x += hgap;
                      }
                      x += d.width;
                      rowHeight = Math.max(rowHeight, d.height);
                   }
                   else {
                      // Start of new row
                      x = d.width;
                      y += vgap + rowHeight;
                      rowHeight = d.height;
                   }
                   reqdWidth = Math.max(reqdWidth, x);
                }
             }
             y += rowHeight;
             y += insets.bottom;
             return new Dimension(reqdWidth+insets.left+insets.right, y);
          }
       }

       private Dimension computeMinSize(Container target) {
          synchronized (target.getTreeLock()) {
             int minx = Integer.MAX_VALUE;
             int miny = Integer.MIN_VALUE;
             boolean found_one = false;
             int n = target.getComponentCount();

             for (int i = 0; i < n; i++) {
                Component c = target.getComponent(i);
                if (c.isVisible()) {
                   found_one = true;
                   Dimension d = c.getPreferredSize();
                   minx = Math.min(minx, d.width);
                   miny = Math.min(miny, d.height);
                }
             }
             if (found_one) {
                return new Dimension(minx, miny);
             }
             return new Dimension(0, 0);
          }
       }

    }

【讨论】:

  • "@author Babu Kalakrishnan - 修改.." 是的。 usenet 的 GUI 大师之一(好吧,无论如何,这就是我认识他们的地方)。 ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-14
  • 1970-01-01
  • 1970-01-01
  • 2012-03-12
  • 2014-04-19
  • 1970-01-01
相关资源
最近更新 更多