由于某些原因,布局似乎不想在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);发布于 2011-05-21 21:52:07
这与选项卡式窗格无关,因为如果您只是将您的TestTab JPanel添加到JFrame的contentPane中,就会出现问题。也许你需要通过设置preferredSize来控制你的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);
}
}发布于 2011-05-21 23:23:02
而不是流到下一个“行”,
听起来Wrap Layout也许能帮上忙。
https://stackoverflow.com/questions/6082097
复制相似问题