MigLayout - 需要有关如何使用停靠参数的帮助(或需要替代方案)
我刚刚开始在 Java 中使用 MigLayout for SWING,到目前为止我真的很喜欢它。但唯一的问题是,码头参数似乎并不像我想象的那样工作,而且我不知道我做错了什么。
问题是:我试图在 JPanel 内添加一个 JButton 并使用 panel.add(button, "east"); 将其停靠在右侧。虽然它实际上使其成为最右边的组件,但它仍然只占用与 flowLayout 中相同的空间。我想要它做的是粘在面板的右侧。
这是一些重现问题的可编译代码:
public class MigLayoutTest extends JFrame
{
public MigLayoutTest()
{
setSize(500,500);
JPanel panel = new JPanel(new MigLayout());
panel.setBackground(Color.YELLOW);
setContentPane(panel);
panel.setSize(500,500);
panel.add(new JButton("Dock east"), "east");
panel.add(new JButton("No dock"));
}
public static void main(String[] args)
{
JFrame frame = new MigLayoutTest();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
输出如下:
这是我想要“dock east”按钮的位置:
如果我使用了错误的参数,我希望有人能告诉我我的情况应该让我的按钮停靠在面板的右侧。
谢谢!
I just started using MigLayout for SWING in Java and I'm really liking it so far. The only thing, though, is that the dock parameters don't seem to work the way I thought they worked and I can't figure out what I'm doing wrong.
The problem is: I'm trying to add a JButton inside a JPanel and docking it to the right side using panel.add(button, "east");. While it actually makes it the rightmost component, it still only takes the same space as it would in a flowLayout. What I'd like it to do is stick to the right side of the panel.
Here's some compilable code that recreates the problem:
public class MigLayoutTest extends JFrame
{
public MigLayoutTest()
{
setSize(500,500);
JPanel panel = new JPanel(new MigLayout());
panel.setBackground(Color.YELLOW);
setContentPane(panel);
panel.setSize(500,500);
panel.add(new JButton("Dock east"), "east");
panel.add(new JButton("No dock"));
}
public static void main(String[] args)
{
JFrame frame = new MigLayoutTest();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Here's what the output looks like:
And here's where I'd want the "dock east" button:
If I'm using the parameters wrong, I'd like it if someone could tell me how I'm supposed to make my button dock to the right side of the panel.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须指定增长参数:
使用它时要小心 - 它可能不会按照您想象的方式工作。这是关于 MigLayout 功能的好读物 http://www.miglayout.com/QuickStart.pdf
You have to specify growth paarameters:
Be careful though how you use it - it may not work the way you think it is. Here is a good read up on MigLayout features http://www.miglayout.com/QuickStart.pdf