- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
这是问题“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/
ValueError Traceback (most recent call last) in 23 out
在 CSS 中,我从来没有真正理解为什么会发生这种情况,但每当我为某物分配 margin-top:50% 时,该元素就会被推到页面底部,几乎完全消失这一页。我假设 50% 时,该元素将位于页面的中间位
我正在尝试在 pyTorch 中训练我的第一个神经网络(我不是程序员,只是一个困惑的化学家)。 网络本身应该采用 1064 个元素向量并用 float 对它们进行评级。 到目前为止,我遇到了各种各样的
我有一个简单的问题。如何在 3 个维度上移动线性阵列?这似乎太有效了,但在 X 和 Y 轴上我遇到了索引问题。我想这样做的原因很简单。我想创建一个带有 block 缓冲区的体积地形,所以我只需要在视口
我正在尝试运行我购买的一本关于 Pytorch 强化学习的书中的代码。 代码应该按照本书工作,但对我来说,模型没有收敛,奖励仍然为负。它还会收到以下用户警告: /home/user/.local/li
我目前正在使用 this repo使用我自己的数据集执行 NLP 并了解有关 CNN 的更多信息,但我一直遇到有关形状不匹配的错误: ValueError: Target size (torch.Si
UIScrollView 以编程方式设置,请不要使用 .xib 文件发布答案。 我的 UIScrollView 位于我的模型类中,所以我希望代码能够轻松导入到另一个项目中,例如。适用于 iPad 或旋
我在我的 Ruby on Rails 应用程序(版本 4.3.1)中使用 Bootstrap gem。我最近发现了响应式字体大小功能 (rfs)。根据 Bootstrap 文档,它刚刚在 4.3 版中
这个问题不太可能帮助任何 future 的访客;它仅与一个小地理区域、一个特定时刻或一个非常狭窄的情况相关,而这些情况通常不适用于互联网的全局受众。如需帮助使这个问题更广泛地适用,visit the
size 之间的语义区别是什么?和 sizeIs ?例如, List(1,2,3).sizeIs > 1 // true List(1,2,3).size > 1 // true Luis 在 c
我想从 div 中删除一些元素属性。我的 div 是自动生成的。我想遍历每个 div 和子 div,并想删除所有 font-size (font-size: Xpx)和 size里面font tag
super ,对 Python 和一般编程 super 新手。我有一个问题应该很简单。我正在使用一本使用 Python 3.1 版的 python 初学者编程书。 我目前正在写书中的一个程序,我正在学
我无法从 NativeBase 更改缩略图的默认大小。我可以显示默认圆圈,即小圆圈和大圆圈,但我想显示比默认大小更大的圆圈。这是我的缩略图代码: Prop 大小不起作用,缩略图仍然很小。 我的 Na
我是pytorch的新手。在玩张量时,我观察到了两种类型的张量- tensor(58) tensor([57.3895]) 我打印了它们的形状,输出分别是 - torch.Size([]) torch
这是我的 docker images 命令的输出: $ docker images REPOSITORY TAG IMAGE ID CREATED
来自 PriorityQueue 的代码: private E removeAt(int i) { assert i >= 0 && i < size; modCount++;
首先,在我的系统上保留以下内容:sizeof(char) == 1 和 sizeof(char*) == 4。很简单,当我们计算下面类的总大小时: class SampleClass { char c
我正在编写一个游戏来查找 2 个图像之间的差异。我创建了 CCSprite 的子类 Spot。首先我尝试创建小图像并根据其位置添加自身,但后来我发现位置很难确定,因为很难避免 1 或 2 个像素的偏移
我有一个 Tumblr Site每个帖子的宽度由标签决定。 如果一篇文章被标记为 #width200,CSS 类 .width200 被分配。 问题是,虽然帖子的宽度不同,但它们都使用主题运算符加载相
这个问题在这里已经有了答案: What is the ideal growth rate for a dynamically allocated array? (12 个答案) 关闭 8 年前。 我
我是一名优秀的程序员,十分优秀!