这是“将组件的首选尺寸保持在 BorderLayout 中心”问题的延续,我不知道如何从这里链接。如果有人想在链接中进行编辑并以某种方式向我发送一条消息,说明它是如何完成的,我将不胜感激。
在那个问题中,我询问并回答了如何将我的组件保持在其首选尺寸,以及如何将位于 BorderLayout 中心的面板放在该布局中心的左上角(而不是让它浮到中间。
我简化了我的真正问题,以便将其放入一个问题中,现在我在概括它时遇到了麻烦。以下代码类似,不同之处在于它在边框布局的中心放置了一个选项卡式窗格。
我已经删掉了一些东西:在我的应用程序中,选项卡式窗格位于拆分窗格的一半中,并且我在上方有一个工具栏(单独的边框布局面板),下方有导航按钮。我把所有这些都删掉了,因为我可以在没有它的情况下演示问题,但这确实意味着我有理由使用边框布局,主要是因为所有这些中心的面板是我想要利用任何额外的面板用户必须给他们的空间。
在这段代码中,我们得到了选项卡式窗格,但是如果窗口给了它们,其中的两个面板会浮动到它们额外空间的中间。在一个版本中(参见代码底部附近的备用行),我们为它们获得滚动条,而在另一个版本中,我们没有。
我希望选项卡式窗格中的组件保持其首选大小。这段代码现在通过将每个窗格放入 gridbaglayout 面板来做到这一点,但这有其他我不想要的效果。
如果每个选项卡式窗格的内容比面板需要的大,我希望每个选项卡式窗格的内容都留在空间的左上角;我希望在空间不足时出现滚动条。
那么对于我如何完成这项工作还有其他意见吗?
package example;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
public class BorderAndBox extends JFrame
{
public static void main(String args[])
{
BorderAndBox bnb = new BorderAndBox();
bnb.createUI();
bnb.setLocationByPlatform(true);
bnb.setVisible(true);
}
public void createUI()
{
JPanel borderPanel = new JPanel(new BorderLayout());
JPanel innerPanelOne = new JPanel();
innerPanelOne.setLayout(new BoxLayout(innerPanelOne, BoxLayout.LINE_AXIS));
String[] firstChoices = { "first", "uno", "UN" };
String[] secondChoices = { "second", "dos", "zwei" };
String[] thirdChoices = { "third", "tres", "drei" };
JComboBox firstCombo = new JComboBox(firstChoices);
JComboBox secondCombo = new JComboBox(secondChoices);
JComboBox thirdCombo = new JComboBox(thirdChoices);
innerPanelOne.add(firstCombo);
innerPanelOne.add(secondCombo);
innerPanelOne.add(thirdCombo);
JPanel innerPanelTwo = new JPanel();
innerPanelTwo.setLayout(new BoxLayout(innerPanelTwo, BoxLayout.PAGE_AXIS));
innerPanelTwo.add(new JLabel("additionalLabel"));
JPanel panelA = new JPanel(new GridBagLayout());
panelA.add(innerPanelOne);
JPanel innerPanelThree = new JPanel();
innerPanelThree.setLayout(new BoxLayout(innerPanelThree, BoxLayout.PAGE_AXIS));
JLabel lblFirst = new JLabel("first in line");
JLabel lblSecond = new JLabel("second to none");
JLabel lblThird = new JLabel("third the bird");
JLabel lblFourth = new JLabel("fourth");
JLabel lblFifth = new JLabel("fifth");
JLabel lblSixth = new JLabel("sixth");
innerPanelThree.add(lblFirst);
innerPanelThree.add(lblSecond);
innerPanelThree.add(lblThird);
innerPanelThree.add(lblFourth);
innerPanelThree.add(lblFifth);
innerPanelThree.add(lblSixth);
JPanel panelB = new JPanel(new GridBagLayout());
panelB.add(innerPanelThree);
JScrollPane scrollPane1 = new JScrollPane(panelA);
JScrollPane scrollPane2 = new JScrollPane(panelB);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add("one", scrollPane1);
tabbedPane.add("two", scrollPane2);
JPanel westPanel = new JPanel(new BorderLayout());
JPanel northPanel = new JPanel(new BorderLayout());
northPanel.add(tabbedPane, BorderLayout.NORTH);
westPanel.add(northPanel, BorderLayout.WEST);
// the following lines are mutually exclusive;
// use only the first, you have no scroll bars,
// use only the second, you do have them
borderPanel.add(westPanel, BorderLayout.CENTER);
// borderPanel.add(tabbedPane, BorderLayout.CENTER);
getContentPane().add(tabbedPane);
pack();
}
}