Swing JTabbedPane 内容
由于某种原因,布局似乎不想在 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);
For some reason the layouts don't seem to want to work inside a JTabbedPane. Instead of flowing onto the next "line", it just acts as if it had infinite horizontal space :( However adding everything directly to the frame without the JTabbedPane works fine...
In my frame:
JTabbedPane tabs = new JTabbedPane(JTabbedPane.TOP);
this.getContentPane().add(this.tabbedPane);
JPanel tab = new TestTab();
tabs.add("Test", tab)
And my TestTab constructor (extends 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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这与选项卡式窗格无关,因为如果您只是将 TestTab JPanel 添加到 JFrame 的 contentPane,就会出现问题。也许您需要通过设置其 PreferredSize 来控制 contentBox 的大小?也许您想使用 GridLayout 而不是 FlowLayout?我自己,我喜欢在这里使用 GridLayout,如下所示:
但是,当发布这样的问题时,请尝试发布可编译的可运行代码,以便我们可以自己看到问题。不要让我们必须自己创建代码,因为您是寻求免费建议的人,因此应该努力帮助其他人轻松帮助您。我要求的是一个像这样的 SSCCE :
This has nothing to do with tabbed panes as your problem will occur if you simply add your TestTab JPanel to the JFrame's contentPane. Perhaps you need to reign in the size of your contentBox Box by setting its preferredSize? Perhaps you want to use a GridLayout rather than a FlowLayout? Myself, I like using a GridLayout here like so:
But also, when posting a problem like this, please try to post compilable runnable code so we can see the problem for ourselves. Don't make us have to create the code ourselves since you're the one asking for the free advice and thus should make an effort to help make it easy for others to help you. What I'm asking for is an SSCCE like this one:
听起来不像换行布局 可能有帮助。
Sounds like the Wrap Layout might help.