由于某种原因,布局似乎不想在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);
答案 0 :(得分: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);
}
}
答案 1 :(得分:2)
而不是流向下一个“线”,
听起来像Wrap Layout可能会有所帮助。