1

何らかの理由で、レイアウトが JTabbedPane 内で機能しないようです。次の「行」に流れる代わりに、無限の水平スペースがあるかのように機能します:(ただし、JTabbedPaneなしですべてをフレームに直接追加するとうまくいきます...

私のフレームでは:

JTabbedPane tabs = new JTabbedPane(JTabbedPane.TOP);
this.getContentPane().add(this.tabbedPane);
JPanel tab = new TestTab();
tabs.add("Test", tab)

そして、私の TestTab コンストラクター (JPanel を拡張します)

contentBox = new Box(BoxLayout.Y_AXIS);

JPanel groupPanel = new JPanel();
groupPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
groupPanel.setBorder(BorderFactory.createTitledBorder("Group"));

//add some paired items to it. The intention is each of these "sub groups"
//should stay together,with the sub groups themselves being liad out left to
//right, top to bottom
for(int i=0; i<10; ++i)
{
    String label = "Button " + i;
    Box itemBox = new Box(BoxLayout.X_AXIS);
    JButton buttonA = new JButton(label + " A");
    JButton buttonB = new JButton(label + " B");
    itemBox.add(buttonA);
    itemBox.add(buttonB);
    groupPanel.add(itemBox);
}

contentBox.add(groupPanel);
//will be more content stuff to be added vertically below,
//suppose will have same issue
this.add(contentBox);
4

2 に答える 2

2

次の「行」に流れる代わりに、

ラップレイアウトが役立つかもしれません。

于 2011-05-21T15:23:02.063 に答える
2

TestTab JPanel を JFrame の contentPane に追加するだけで問題が発生するため、これはタブ付きペインとは関係ありません。おそらく、preferredSize を設定して contentBox ボックスのサイズを調整する必要がありますか? おそらく、FlowLayout ではなく GridLayout を使用したいですか? 私自身、ここで GridLayout を次のように使用するのが好きです。

  JPanel groupPanel = new JPanel();
  //!! groupPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
  groupPanel.setLayout(new GridLayout(0, 2, 5, 5));

ただし、このような問題を投稿する場合は、コンパイル可能な実行可能なコードを投稿して、問題を自分で確認できるようにしてください。無料のアドバイスを求めているのはあなたであり、他の人があなたを助けやすくするために努力する必要があるため、自分でコードを作成する必要はありません. 私が求めているのは、次のようなSSCCEです。

import java.awt.*;
import javax.swing.*;

public class TestTabsTest {
   private static void createAndShowUI() {
      JTabbedPane tabs = new JTabbedPane(JTabbedPane.TOP);
      JPanel tab = new TestTab();
      tabs.add("Test", tab);

      JFrame frame = new JFrame("TestTabsTest");
      frame.getContentPane().add(tabs);
      //frame.getContentPane().add(new TestTab());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

class TestTab extends JPanel {
   private Box contentBox;

   public TestTab() {
      contentBox = new Box(BoxLayout.Y_AXIS);
      //contentBox.setPreferredSize(new Dimension(600, 600));

      JPanel groupPanel = new JPanel();
      //!! groupPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
      groupPanel.setLayout(new GridLayout(0, 2, 5, 5));
      groupPanel.setBorder(BorderFactory.createTitledBorder("Group"));

      // add some paired items to it. The intention is each of these
      // "sub groups"
      // should stay together,with the sub groups themselves being liad out left
      // to
      // right, top to bottom
      for (int i = 0; i < 10; ++i) {
         String label = "Button " + i;
         Box itemBox = new Box(BoxLayout.X_AXIS);
         JButton buttonA = new JButton(label + " A");
         JButton buttonB = new JButton(label + " B");
         itemBox.add(buttonA);
         itemBox.add(buttonB);
         groupPanel.add(itemBox);
      }

      contentBox.add(groupPanel);
      // will be more content stuff to be added vertically below,
      // suppose will have same issue
      this.add(contentBox);
   }
}
于 2011-05-21T13:52:07.130 に答える