如何固定 Swing 组件的边界
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您有自定义布局管理器,那么您的布局管理器应遵循面板的首选大小。问题出在您的布局管理代码上。
或者,对于简单的解决方案,将自定义面板添加到使用 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.
感谢
Thomas
和camickr
的建议,我使用下面的语法行实现了解决方案。再次感谢大家的建议。
Thanks to
Thomas
andcamickr
for their recommendations, I have achieved a solution using the line of syntaxes below.Again thanks to all for the recommendations.