miglayout:如何忽略组件高度
有没有办法告诉 MigLayout 忽略组件的高度?
这是一个测试示例:
我有一个超大组件(右上角的“大”按钮)第一行,和一个 JPanel,它有一个或多或少的三角形形状,横跨整个第二行。
我想让 MigLayout 在选择第一行的大小时忽略“大”按钮的高度,因为我知道它可以与我的第二行组件重叠。
我该怎么做?
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class IgnoreComponentHeight {
public static void main(String[] args) {
JFrame jf = new JFrame("ignore component height test");
JPanel p = new JPanel();
p.setLayout(new MigLayout("","[] [] [] []", ""));
p.add(new JButton("one"), "");
p.add(new JButton("two"), "");
p.add(new JButton("three"), "");
JButton big = new JButton("big");
big.setPreferredSize(new Dimension(40,80));
p.add(big, "wrap");
JPanel tripanel = new JPanel();
tripanel.setLayout(new MigLayout("","[] [] [] []", ""));
int k = 0;
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j <= i; ++j)
{
tripanel.add(new JButton("tri"+k), j == i ? "wrap" : "");
++k;
}
}
p.add(tripanel, "span,wrap");
jf.setContentPane(p);
jf.pack();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
Is there any way to tell MigLayout to ignore a component's height?
Here's a test example:
I have an oversized component (the "big" button in the upper right) in the first row, and a JPanel which has a more-or-less triangular shape that spans the whole 2nd row.
I would like to have MigLayout ignore the height of the "big" button when choosing a size for the first row, because I know it's ok for it to overlap with my 2nd-row component.
How can I do this?
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class IgnoreComponentHeight {
public static void main(String[] args) {
JFrame jf = new JFrame("ignore component height test");
JPanel p = new JPanel();
p.setLayout(new MigLayout("","[] [] [] []", ""));
p.add(new JButton("one"), "");
p.add(new JButton("two"), "");
p.add(new JButton("three"), "");
JButton big = new JButton("big");
big.setPreferredSize(new Dimension(40,80));
p.add(big, "wrap");
JPanel tripanel = new JPanel();
tripanel.setLayout(new MigLayout("","[] [] [] []", ""));
int k = 0;
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j <= i; ++j)
{
tripanel.add(new JButton("tri"+k), j == i ? "wrap" : "");
++k;
}
}
p.add(tripanel, "span,wrap");
jf.setContentPane(p);
jf.pack();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您正在寻找的是“spany 2”或绝对定位,例如“pos Visual.x2-pref-5 5”。
查看 MigLayout 网页并加载 swing 演示。然后进入绝对定位部分。您可以右键单击组件并尝试其中的约束,这将帮助您找到真正想要的东西。
Maybe what you're looking for is "spany 2" or absolute positioning like "pos visual.x2-pref-5 5".
Check out the MigLayout web page and load up the swing demo. Then go to the absolute positioning section. You can right click on the components and experiment with the constraints there, which will help you dial into what you really want.