首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swing JTabbedPane内容

Swing JTabbedPane内容
EN

Stack Overflow用户
提问于 2011-05-21 21:40:33
回答 2查看 3.8K关注 0票数 1

由于某些原因,布局似乎不想在JTabbedPane中工作。而不是流到下一个“行”,它只是表现得好像它有无限的水平空间:(然而,不使用JTabbedPane直接将所有内容添加到帧中效果很好……

在我的框架中:

代码语言:javascript
复制
JTabbedPane tabs = new JTabbedPane(JTabbedPane.TOP);
this.getContentPane().add(this.tabbedPane);
JPanel tab = new TestTab();
tabs.add("Test", tab)

和我的TestTab构造函数(扩展了JPanel)

代码语言:javascript
复制
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);
EN

回答 2

Stack Overflow用户

发布于 2011-05-21 21:52:07

这与选项卡式窗格无关,因为如果您只是将您的TestTab JPanel添加到JFrame的contentPane中,就会出现问题。也许你需要通过设置preferredSize来控制你的contentBox盒子的大小?也许您想使用GridLayout而不是FlowLayout?我自己喜欢在这里使用GridLayout,如下所示:

代码语言:javascript
复制
  JPanel groupPanel = new JPanel();
  //!! groupPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
  groupPanel.setLayout(new GridLayout(0, 2, 5, 5));

但是,当发布这样的问题时,请尝试发布可编译的可运行代码,以便我们可以自己看到问题。不要让我们自己创建代码,因为您是寻求免费建议的人,因此应该努力帮助其他人轻松帮助您。我要求的是一个这样的SSCCE

代码语言:javascript
复制
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);
   }
}
票数 2
EN

Stack Overflow用户

发布于 2011-05-21 23:23:02

而不是流到下一个“行”,

听起来Wrap Layout也许能帮上忙。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6082097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档