Swing JTabbedPane содержимое - PullRequest
1 голос
/ 21 мая 2011

По какой-то причине макеты не хотят работать внутри 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);

Ответы [ 2 ]

2 голосов
/ 21 мая 2011

Вместо того, чтобы переходить на следующую «строку»,

Похоже, Wrap Layout может помочь.

2 голосов
/ 21 мая 2011

Это не имеет ничего общего с панелями с вкладками, поскольку ваша проблема возникнет, если вы просто добавите свой TestTab JPanel в панель содержимого JFrame. Возможно, вам нужно изменить размер вашего поля ContentBox, установив его предпочитаемый размер? Возможно, вы хотите использовать 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);
   }
}
...