質問

私はjpanelを持っています、そして、私は動的に、JCheckboxの内部を作成します。これらは、常に並んでjcheckboxを追加する必要があります。サイドに挿入するスペースが増える場合は、単純なテキストエディターのように、JCheckboxesの新しいラインが作成されます。

これは完全に起こっています。だが ...

このJPanelのレイアウトをFlowLayoutに設定します。

明らかな問題は、窓にスペースが限られていることです。したがって、これに対する優れた解決策は、JScrollpaneにこのJPanelを挿入し、垂直スクロールでのみ実現することです。しかし、私には問題があります。垂直スクロールバーのみを作成することはできますが、アイテムは常に「永遠に」並んで追加されます。そして、垂直スクロールは単に動作せず、水平方向にのみ機能します。

巻物を垂直にのみ作成するために多くの方法を試しましたが、何も機能しませんでした(それが機能していれば、私はここにいませんでした:])。

だから、誰かが同様の問題を抱えていて、私を助けることができますか?

私を助けてくれる人たちにとても感謝します。

もういや。

役に立ちましたか?

解決

ScrollpanesとFlowLayoutsでも同じ問題を扱いました。最良の解決策は、垂直方向の変更を考慮したFlowLayoutの変更されたバージョンを使用することです。このようなレイアウトのコードは次のとおりです。プロジェクトに含めて、FlowLayoutのように呼ぶことができますが、実際にはScrollpaneでうまく機能します。

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

    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top