编程语言
首页 > 编程语言> > java – 第2轮:使用滚动和选项卡式窗格在边框布局中保持组件首选大小

java – 第2轮:使用滚动和选项卡式窗格在边框布局中保持组件首选大小

作者:互联网

这是问题“Keeping preferred sizes of components in center of BorderLayout”的延续,我不知道如何从这里链接.如果有人想要在链接中编辑并向我发送消息以某种方式说明它是如何完成的,我会很感激.

在那个问题中,我询问并回答了如何将我的组件保持在他们喜欢的尺寸,并作为附件如何将位于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();
    }



}

解决方法:

也许我错过了你想要做的事情,但你似乎缺少的是codE,它设置GridBagConstraints为你的GridBagLayout使用容器添加一个组件.例如,

  GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
        GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, 
        new Insets(0, 0, 0, 0), 0, 0);
  panelA.add(innerPanelOne, gbc);

  //...

  panelB.add(innerPanelThree, gbc);

标签:java,swing,layout-manager,border-layout
来源: https://codeday.me/bug/20190626/1291697.html