Java Swing:如何为组件定义一个最小维度(两个维度中的一个)?

发布于 2024-09-26 09:13:22 字数 372 浏览 0 评论 0原文

要成功定义最小尺寸,我必须执行以下操作:

// setting minimal width AND height
Dimension min = new Dimension(100, 100);
comp.setMinimumSize(min);
comp.setPreferredSize(min);
comp.setSize(min);

当我留下一行时,它不起作用,这很奇怪,但这不是重点。

我该如何限制两个尺寸(宽度高度)中的一个,并让组件和/或布局管理器自动决定未指定强>维度?

当我为不想限制的尺寸使用非常小的值时,许多组件显示错误(即太小)。

To define minimal size successfully, I have to do the following:

// setting minimal width AND height
Dimension min = new Dimension(100, 100);
comp.setMinimumSize(min);
comp.setPreferredSize(min);
comp.setSize(min);

When I left one line out it doesn't work, which is strange, but it's not the point.

What do I do to limit just one of two dimensions (width or height) and let the component and/or the layout manager decide automatically about the unspecified dimension?

When I use a very small value for that dimension which I don't want to limit, many components are displayed wrong (i.e. too small).

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

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

发布评论

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

评论(2

难忘№最初的完美 2024-10-03 09:13:22

默认情况下(即,如果未在组件上调用 setMinimumSize),getMinimumSize 委托给组件的布局管理器,因此您可以尝试重新定义 getMinimumSize > 方法如下:

public Dimension getMinimumSize()
{
    return new Dimension(minWidth, super.getMinimumSize().height);
}

如果您这样做,请记住您不应在组件上调用 setMinimumSize

By default (i.e. if setMinimumSize has not been called on the component) getMinimumSize delegates to the component's layout manager, so you can try to redefine the getMinimumSize method as follows:

public Dimension getMinimumSize()
{
    return new Dimension(minWidth, super.getMinimumSize().height);
}

If you do this, remember that you should not call setMinimumSize on the component.

℡Ms空城旧梦 2024-10-03 09:13:22

我自己提出了一种方法:

对于将所有内容对齐一个轴的布局管理器(例如 BoxLayout),可以将“未定义的维度”设置为 Integer.MAX。这很奇怪,但它确实有效。似乎与对齐轴相反的轴被忽略(除非它太小,如问题中提到的)。

private final static int UNSPECIFIED_DIMENSION = Integer.MAX_VALUE;

public static void setMinimalDimension(Component comp, int width, int height) {
  Dimension dim = new Dimension(width, height);
  comp.setMinimumSize(dim);
  comp.setPreferredSize(dim);
  comp.setSize(dim);
}

public static void setMinimalWidth(Component comp, int width) {
  setMinimalDimension(comp, width, UNSPECIFIED_DIMENSION);
}

public static void setMinimalHeight(Component comp, int height) {
  setMinimalDimension(comp, UNSPECIFIED_DIMENSION, height);
}

如前所述,可与轴对齐布局管理器一起使用。

是的,Java Swing 还有另一个谜团......

I propose an approach by myself:

For layout managers which align everything one axis (such as BoxLayout), one can specify "undefined dimension" set to Integer.MAX. It is strange, but it works. It seems that the axis which is opposite to the alignment axis is ignored (unless it's too small, as mentioned in the question).

private final static int UNSPECIFIED_DIMENSION = Integer.MAX_VALUE;

public static void setMinimalDimension(Component comp, int width, int height) {
  Dimension dim = new Dimension(width, height);
  comp.setMinimumSize(dim);
  comp.setPreferredSize(dim);
  comp.setSize(dim);
}

public static void setMinimalWidth(Component comp, int width) {
  setMinimalDimension(comp, width, UNSPECIFIED_DIMENSION);
}

public static void setMinimalHeight(Component comp, int height) {
  setMinimalDimension(comp, UNSPECIFIED_DIMENSION, height);
}

Works, as mentioned, with axis-aligning layout managers.

Yes, there goes another mystery of Java Swing...

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