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

而不是流入下一条“线”,

听起来像Wrap Layout可能会有所帮助。

于 2011-05-21T15:23:02.063 回答
2

这与选项卡式窗格无关,因为如果您只是将 TestTab JPanel 添加到 JFrame 的 contentPane 中,就会出现问题。也许你需要通过设置它的preferredSize 来控制你的contentBox Box 的大小?也许您想使用 GridLayout 而不是 FlowLayout?我自己,我喜欢在这里使用 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 回答