如何固定 Swing 组件的边界

发布于 2024-12-08 15:03:27 字数 690 浏览 1 评论 0原文

我有一个 JPanel,其中添加了一些组件(复选框、组合)。我注意到,当框架最大化时,组件的边界也会向右移动或移动。然后,在恢复时,组件会移回原始位置。

在 21 英寸及以上的显示器上,组件移动确实会产生影响,因为您可以看到组件移动到最右侧。

我们正在使用实现java.awt.LayoutManager2的自定义布局管理器。类内容非常庞大,因此将指向确定组件边界的区域。

protected int hMargin = 0;
..
Insets insets = target.getInsets();
Dimension size = target.getSize();

int  x = (size.width - insets.left - insets.right - 15 * hMargin);

从调用布局的框架中添加组件,如下所示:

JPanel  pl = new JPanel(new OurLayout(this))
//add the component to panel
pl.add(checkBox);
..

在我们决定 x 的位置,我想添加一行以防止组件在调用布局的框架时移动添加的面板已最大化。

谁能提出关于如何实现这一目标的任何想法?示例代码将不胜感激。

I have a JPanel to which I have added a few components (checkbox, combo etc.). I have noticed that, when the frame is maximized, the bounds of components also move or shift to the right. Then, on restore the components are shifted back to the original position.

On 21 and above inch monitor, the components shift really makes a difference as you can see the components move far right.

We are using customized layout manager which implements java.awt.LayoutManager2. The class content is quiet huge, so will point to the areas which determine the bounds for the components.

protected int hMargin = 0;
..
Insets insets = target.getInsets();
Dimension size = target.getSize();

int  x = (size.width - insets.left - insets.right - 15 * hMargin);

And from the frame which calls the layout and add the components as shown below:

JPanel  pl = new JPanel(new OurLayout(this))
//add the component to panel
pl.add(checkBox);
..

At the point where we decide x, I want to add a line to prevent the components from shifting when the frame on which the calling panel is added is maximized.

Can anyone suggest any ideas on how to achieve this? example code will be well appreciated.

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

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

发布评论

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

评论(2

我的痛♀有谁懂 2024-12-15 15:03:27

如果您有自定义布局管理器,那么您的布局管理器应遵循面板的首选大小。问题出在您的布局管理代码上。

或者,对于简单的解决方案,将自定义面板添加到使用 FlowLayout 的另一个面板。然后您的自定义面板的首选尺寸将得到尊重。

If you have a custom layout manager, then your layout manager should respect the preferred size of the panel. The problem is with your layout management code.

Or for a simple solution add your custom panel to another panel that uses a FlowLayout. Then the preferred size of your custom panel will be respected.

囚你心 2024-12-15 15:03:27

感谢 Thomascamickr 的建议,我使用下面的语法行实现了解决方案。

      size=c.getMinimumSize();
      int dist =(size.width - insets.left - insets.right);
      int move = ((dist > 32) ? 35 : 38);
      x = (dist + move * hMargin);

再次感谢大家的建议。

Thanks to Thomas and camickr for their recommendations, I have achieved a solution using the line of syntaxes below.

      size=c.getMinimumSize();
      int dist =(size.width - insets.left - insets.right);
      int move = ((dist > 32) ? 35 : 38);
      x = (dist + move * hMargin);

Again thanks to all for the recommendations.

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