java两个组件使用MigLayout相互重叠
我试图得到两个组件
frame.getContentPane().setLayout(new MigLayout("", "[53px,grow][57px][grow]", "[23px][][][]"));
JTextPane itemTitle = new JTextPane();
frame.getContentPane().add(itemTitle,"cell 0 4,alignx left,aligny top");
itemTitle.setVisible(true);
itemTitle.setMinimumSize(new Dimension(50, 50));
List choices = new List();
frame.add(choices, "cell 0 4,alignx left,aligny top");
choices.setVisible(true);
在同一个地方,但发生的一切是:
itemTitle和choices中突出显示的两个组件。 我的目标是让上面的按钮将一个“setVisible”设置为true,另一个设置为false。他们永远不会都是真的。如何在一个单元格中同时获得两个组件?它也把我上面的按钮放错了位置,我不太清楚为什么。我把这两个组件的重要代码放在上面,如果您要求,我可以把完整的GUI代码放在上面
我发现:Fill Entire Cell With Two Components Using MigLayout但是它已经过时了,老实说,我不明白解决方法。 我在不断学习,我以前从未使用过MigLayout。我应该使用不同的布局吗
谢谢你的帮助
# 1 楼答案
不,坚持使用
MigLayout
。您选择了正确的布局管理器。 我建议你花点时间学习这位经理;创造夫妇 在较小的实际示例中,了解此 经理提供MigLayout
是一个基于网格的布局管理器。(其最重要的模式是。) 新按钮和打开按钮之间的间隙是由于 高亮显示的组件和新按钮构成一列。列宽 由最大单元格的宽度确定。要绕过这一点,我们可以 使用split
约束。(通常与span
约束结合使用。) 在我的示例中,我使用此技术将两个按钮居中放置在 当前可见的标签。 如果我们对布局中的某些内容不确定,可以使用debug
绘制网格线和边界的布局约束 组件的名称不要以像素为单位设置边界。你没有利用其中一个 此管理者的最重要优势是独立于决议和 新闻部。像素边界不可移植。在较小的屏幕上,按钮之间的3倍间隙看起来可以,但在较大的屏幕上则不可接受。
MigLayout
提供了多个选项供选择,包括相关和不相关的间隙、逻辑像素、点或厘米通常不需要设置零部件的最小尺寸。但如果我们需要 这样做,我们就有了
wmin
和wmax
约束。这应该由政府来完成 布局管理器,而不是它之外的代码。集合(最小|最大|首选)大小 应该避免使用这种方法。(然而,对于较差的管理者,我们不能没有他们。) 同样,以像素为单位设置尺寸并不是最优的现在我们来解决问题
MigLayout
具有hidemode
处理约束 符合你的要求。有四种隐藏模式。我想我们需要hidemode 3
,其中所有不可见组件都不参与 布局我们的示例有两个按钮和五个标签。按钮会动态地更改其属性 能见度
# 2 楼答案
然后您应该使用包含这两个组件的JPanel。然后在该面板上使用Card Layout,并使用
CardLayout
确定在任何给定时间哪个组件是可见的。与任何其他组件一样,可以使用MigLayout将此面板添加到面板中在我看来,您正在使用AWT组件。您应该对Swing应用程序使用
JList