7

Swing GUI で一部のコンポーネントのサイズを変更する方法がわかりません。一部のカスタム ラベルが FlowLayout に追加されており、ダイアログのサイズを変更するときに本来の動作をしません。パネルは、jgoodies フォーム フレームワークを使用して構築されています。

これを使用する場合、FlowLayout が xy(3, y) に追加されている場所

FormLayout layout = new FormLayout("r:d, 5px, f:d:g", // columns
            "p, p, 5px, p, 5px, p") // rows

FlowLayout が展開され、スクロール バーが表示されます
tags_scroll

使用する場合

FormLayout layout = new FormLayout("r:d, 5px, f:10:g", // columns
            "p, p, 5px, p, 5px, p") // rows

FlowLayout は使用可能なスペースを使用し、2 行目の項目は非表示になります
tags_vanish

FlowLayout を含む各行の高さをコンポーネントの現在の高さに拡張したいと思います。残念ながら、優先サイズは常に単一行の高さに対応します。
別のレイアウトの方が適切でしょうか? 左側の太字のテキストを右揃えにし、その後に FlowLayout を配置する必要があります。

ソース

[編集]何週間もこれを行う方法を見つけようとした後、実際の質問は次のようになります
。JPanel に一連のラベルが追加されています。この JPanel は、使用可能なすべてのスペースを水平方向 (ダイアログ サイズからタグ名ラベルの幅を差し引いたもの) を使用し、必要に応じて垂直方向に拡張する必要があります。JPanel の高さがダイアログよりも大きくなると、垂直スクロール バーが表示されます (水平スクロール バーは表示されません)。ダイアログには複数の JPanel を表示でき、それらは次々に (垂直方向に) 表示されます。

GridBagLayout とWrapLayoutを使用した試みを次に示します。

public class GridBagLayoutTagPanel extends JPanel {
private static final long serialVersionUID = -441746014057882848L;
private final int NB_TAGS = 5;

public GridBagLayoutTagPanel() {
    setLayout(new GridLayout());

    JPanel pTags = new JPanel(new GridBagLayout());
    pTags.setBackground(Color.ORANGE);
    GridBagConstraints c = new GridBagConstraints();
    c.ipadx = 5;
    c.ipady = 5;

    int rowIndex = 0;
    for (int i = 0; i < NB_TAGS; i++) {
        //add tag name
        JLabel lTagName = new JLabel(String.format("Tag %s:", i));
        lTagName.setFont(lTagName.getFont().deriveFont(Font.BOLD));
        c.fill = GridBagConstraints.NONE;
        c.gridx = 0;
        c.gridy = rowIndex++;
        pTags.add(lTagName, c);

        //add tag values
        JPanel pTag = new JPanel(new BorderLayout());
        pTag.add(new JLabel("+"), BorderLayout.LINE_START); //label used to add new tags
        pTag.add(getWrapPanel(), BorderLayout.CENTER); //the list of tag values
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        pTags.add(pTag, c);
    }

    //JScrollPane sp = new JScrollPane(pTags);
    //sp.setBorder(BorderFactory.createEmptyBorder());

    add(pTags);
    }

private static JPanel getWrapPanel() {
   JPanel p = new JPanel(new WrapLayout(FlowLayout.LEFT, 5, 0));

   for (int i = 0; i < 50; i++) {
           p.add(new JLabel("t" + i));
   }
   return p;
}

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.getContentPane().add(new GridBagLayoutTagPanel());
    f.setSize(new Dimension(500, 300));
    f.setVisible(true);
}
}
4

3 に答える 3

11

FlowLayout を含む各行の高さをコンポーネントの現在の高さに拡張したいと思います。残念ながら、優先サイズは常に単一行の高さに対応します。

Wrap Layoutがこれを処理します。

于 2012-01-12T23:29:00.403 に答える
3

Rob の助けと彼のWrapPanelScrollablePanelを使用することで、正確に機能するようになりました。

public class GridBagLayoutTagPanel extends JPanel {
  private final int NB_TAGS = 5;
  private final int NB_TAGVALUES = 50;

  public GridBagLayoutTagPanel() {
    setLayout(new GridLayout());

    ScrollablePanel pTags = new ScrollablePanel(new GridBagLayout());
    pTags.setScrollableWidth(ScrollablePanel.ScrollableSizeHint.FIT);
    pTags.setBackground(Color.ORANGE);
    GridBagConstraints c = new GridBagConstraints();
    c.ipadx = 5;
    c.ipady = 5;

    int rowIndex = 0;
    for (int i = 0; i < NB_TAGS; i++) {
      // add tag name
      JLabel lTagName = new JLabel(String.format("Tag %s:", i));
      lTagName.setFont(lTagName.getFont().deriveFont(Font.BOLD));
      c.fill = GridBagConstraints.NONE;
      c.gridx = 0;
      c.gridy = rowIndex++;
      c.weightx = 0; // keep minimum size of the cell containing the label when resizing
      c.weighty = 0;
      pTags.add(lTagName, c);

      // add tag values
      JPanel pTag = new JPanel(new BorderLayout());
      pTag.add(new JLabel("+"), BorderLayout.LINE_START); // label used to add new tags
      pTag.add(getWrapPanel(), BorderLayout.CENTER); // the list of tag values
      c.fill = GridBagConstraints.HORIZONTAL;
      c.gridx = 1;
      c.weightx = 1; // fill horizontally
      c.weighty = 1;
      pTags.add(pTag, c);
   }

   JScrollPane sp = new JScrollPane(pTags);
   sp.setBorder(BorderFactory.createEmptyBorder());

   add(sp);
  }

  private JPanel getWrapPanel() {
    JPanel p = new JPanel(new WrapLayout(FlowLayout.LEFT, 5, 0));

    for (int i = 0; i < NB_TAGVALUES; i++) {
      p.add(new JLabel("t" + i));
    }

    p.setSize(400, 1);
    return p;
  }

  public static void main(String[] args) {
    JFrame f = new JFrame();
    f.getContentPane().add(new GridBagLayoutTagPanel());
    f.setSize(new Dimension(500, 300));
    f.setVisible(true);
  }
}
于 2012-02-01T17:06:35.073 に答える
2

MigLayoutを試しましたか? これは私が使用する唯一のレイアウト マネージャーであり、実行できないレイアウトを見つけたことはありません。

于 2012-01-31T04:27:56.740 に答える