gpt4 book ai didi

java - 第 2 轮 : keeping components preferred sizes in border layout, 带有滚动和选项卡式 Pane

转载 作者:搜寻专家 更新时间:2023-11-01 02:51:56 26 4
gpt4 key购买 nike

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

在那个问题中,我询问并回答了如何使我的组件保持其首选大小,以及如何将位于 BorderLayout 中心的面板放在该布局中心的左上角(而不是让它漂浮到中间。

我简化了我的实际问题以便将其放入问题中,但现在我无法概括它。下面的代码是类似的,只是它在边框布局的中心放置了一个选项卡式 Pane 。

我删除了一些内容:在我的应用程序中,选项卡式 Pane 位于拆分 Pane 的一半中,上方有一个工具栏(单独的边框布局面板),下方有导航按钮。我从这里删除了所有内容,因为我可以在没有它的情况下演示问题,但这确实意味着我有使用边框布局的原因,主要是因为所有这些中心的面板是我想利用任何额外的面板用户必须给他们的空间。

在这段代码中,我们得到了选项卡式 Pane ,但是其中的两个面板 float 到它们额外空间的中间,如果窗口给了它们的话。在一个版本中(查看代码底部附近的交替行)我们为它们提供了滚动条,而在另一个版本中我们没有。

我希望选项卡式 Pane 中的组件保持其首选大小。这段代码现在通过将每个 Pane 放入一个网格布局面板来做到这一点,但这会产生我不想要的其他效果。

如果每个选项卡式 Pane 的内容大于面板所需,我希望它保留在空间的左上角;我希望在空间不足时显示滚动条。

那么对于我如何完成这项工作还有其他意见吗?

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();
}



}

最佳答案

也许我遗漏了您想要做的事情,但您似乎遗漏的是设置 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 - 第 2 轮 : keeping components preferred sizes in border layout, 带有滚动和选项卡式 Pane ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9542348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com