MigLayout - 需要有关如何使用停靠参数的帮助(或需要替代方案)

发布于 2024-11-19 02:36:48 字数 1043 浏览 2 评论 0原文

我刚刚开始在 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);
  }
}

输出如下: MigLayoutTest result

这是我想要“dock east”按钮的位置: MigLayoutTest预期结果

如果我使用了错误的参数,我希望有人能告诉我我的情况应该让我的按钮停靠在面板的右侧。

谢谢!

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:
MigLayoutTest result

And here's where I'd want the "dock east" button:
MigLayoutTest expected result

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各自安好 2024-11-26 02:36:48

您必须指定增长参数

new MigLayout("", "[grow]", "[]")

使用它时要小心 - 它可能不会按照您想象的方式工作。这是关于 MigLayout 功能的好读物 http://www.miglayout.com/QuickStart.pdf

You have to specify growth paarameters:

new MigLayout("", "[grow]", "[]")

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文