删除 MigLayout 中的 JButton 填充

发布于 2024-08-30 17:15:53 字数 342 浏览 11 评论 0原文

我有一个像这样的小“弹出窗口”:

Tiny Button

但我不想要按钮周围的填充,我想要它与所提供的文本一样小。

btn.setMargin(new Insets(1, 1, 1, 1));
panel.add(lbl, "wrap");
panel.add(btn);
frame.pack();
frame.setVisible(true);

至少这行不通…… 这可以在 MigLayout 上实现吗?或者我应该为此框架使用其他布局管理器。

I have a small "popup" like this:

Tiny button

But I don't want the padding around the button, I want it to be as small as it can be with the supplied text.

btn.setMargin(new Insets(1, 1, 1, 1));
panel.add(lbl, "wrap");
panel.add(btn);
frame.pack();
frame.setVisible(true);

At least this doesn't work...
Can this be achieved on MigLayout or should I use some other layout manager for this frame.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

眼眸印温柔 2024-09-06 17:15:53

您构建它时使用的 MigLayout 约束是什么?如果您还没有尝试过,您可能想尝试使用 novisualpadding。此外,debug 布局约束将在组件周围绘制方框以显示它们占用的空间。 (我认为红色虚线是组件的大小,蓝色虚线是组件所在的“单元格”。)

// old version
// JPanel panel = new JPanel(new MigLayout());

// new verion
JPanel panel = new JPanel(new MigLayout("debug, novisualpadding"));

我经常使用 MigLayout,并发现“备忘单”非常有用。 http://migcalendar.com/miglayout/cheatsheet.html

What's your MigLayout constraints that it was built with? You might want to try using novisualpadding if you aren't already. Also, the debug layout constraint will draw boxes around your components to show what room they take up. (I think the red dotted lines are the size of the components and the blue dotted lines are the "cells" that the component is in.)

// old version
// JPanel panel = new JPanel(new MigLayout());

// new verion
JPanel panel = new JPanel(new MigLayout("debug, novisualpadding"));

I use MigLayout pretty often and have found the "cheat sheet" pretty useful. http://migcalendar.com/miglayout/cheatsheet.html

戒ㄋ 2024-09-06 17:15:53

布局管理器不应该影响组件的绘制方式。

使用 Metal LAF 时,setMargin(...) 对我来说效果很好。也许您的 LAF 不支持 setMargin() 方法。或者也许您在其他地方有一些代码覆盖了按钮的首选大小,导致它像这样绘制。

但是,如果 MIG 布局出现问题,您可以尝试 换行布局

A layout manager should not affect the way a component is painted.

The setMargin(...) works fine for me when using the Metal LAF. Maybe your LAF doesn't support the setMargin() method. Or maybe you have some code elsewhere that is overriding the preferred size of the button causing it to paint like this.

However, if the MIG layout turns out to be the problem, you could try the Wrap Layout.

说谎友 2024-09-06 17:15:53

以 MIke 的回答作为初创公司,我在 MIG“CheatSheet”中找到了解决您问题的可能解决方案。

设置 MIGLayout 时,可以使用定义列和行的方括号之间的整数指定组件之间的间隙。

例如

MigLayout buttonMig = new MigLayout("", "[139px,grow]0[179px,grow]50[142px,grow][143px,grow]", "0[60px:60px:60px,center]");

Taking MIke's answer as a startup, I found in the MIG "CheatSheet" a possible solution to your problem.

When you set up a MIGLayout, you can specify the gap between component with an integer between the square brackets defining columns and rows.

E.g.

MigLayout buttonMig = new MigLayout("", "[139px,grow]0[179px,grow]50[142px,grow][143px,grow]", "0[60px:60px:60px,center]");
稚然 2024-09-06 17:15:53

我刚刚也遇到这个问题了。我发现以下方法对我有用:

this.add(okButton, "width pref:pref:pref");

或者

this.add(okButton, "wmax pref");

在使用物质外观和感觉时,有一个功能,即最小尺寸总是太宽。这可以用这个不起眼的 hack 来覆盖。

okButton.putClientProperty(
    SubstanceLookAndFeel.BUTTON_NO_MIN_SIZE_PROPERTY, Boolean.TRUE);

I've just been having this issue too. I discovered that the following worked for me:

this.add(okButton, "width pref:pref:pref");

or

this.add(okButton, "wmax pref");

Also when using the substance look and feel there is a feature where the minimum size is always too wide. This can be overridden with this obscure hack.

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