Java Swing 中放置 JSeparator 后的间隙大小
我在 Java Swing 中遇到一个简单的问题。我将代码简化为以下代码片段。我不确定如何最小化水平 JSeperator 与下一个 JTextField 之间的间隙大小,因为当前代码在两者之间产生巨大间隙。
GroupLayout layout = new GroupLayout(jPanel1);
jPanel1.setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup()
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createSequentialGroup()
.addComponent(button)
))
.addComponent(jSeparator)
.addComponent(jTextField)
);
layout.setVerticalGroup(layout.createSequentialGroup()
.addComponent(button)
.addComponent(jSeparator)
.addComponent(jTextField)
);
一般来说,如何将间隙大小控制为任何整数表示的值,而不是使用 addPreferredGap
?
谢谢。
好的,这是根据上面发布的代码生成的窗口:
您可以看到 JSeparator 和 JTextField 之间的空间非常宽。
I have a simple problem here in Java Swing. I simplified my code to the following snippet. I am not sure how I can minimize the gap size between the horizontal JSeparator with the next JTextField, as current code generates a huge gap between the two.
GroupLayout layout = new GroupLayout(jPanel1);
jPanel1.setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup()
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createSequentialGroup()
.addComponent(button)
))
.addComponent(jSeparator)
.addComponent(jTextField)
);
layout.setVerticalGroup(layout.createSequentialGroup()
.addComponent(button)
.addComponent(jSeparator)
.addComponent(jTextField)
);
And also in general, how can I control the gap size to any integer represented value, instead of using the addPreferredGap
?
Thank you.
Okay, this is the window generated from the code posted above:
You can see the space between the JSeparator and JTextField is very wide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有 sscce,问题似乎出在您未显示的代码中。可能涉及父容器的布局或
pack()
。注意,JFrame
的默认布局是BorderLayout
;默认位置是CENTER
。这是一个用于比较您的代码的 sscce 。附录:评论您的
GroupLayout
面板的父级是另一个JPanel
,您提出了以下问题,是的,给封闭的
JPanel
一个合适的布局,例如GridLayout
,如下所示。在这方面,后者的行为与JFrame
的BorderLayout.CENTER
非常相似。Absent your sscce, the problem appears to be in the code you don't show. The parent container's layout or
pack()
may be involved. Note that the default layout ofJFrame
isBorderLayout
; the default position isCENTER
. Here's an sscce with which to compare your code.Addendum: Commenting that the parent of your
GroupLayout
panel is anotherJPanel
, you asked the following,Yes, give the enclosing
JPanel
a suitable layout, e.g.GridLayout
as shown below. The latter behaves much like theBorderLayout.CENTER
of theJFrame
in this regard.在垂直布局中,按以下方式添加分隔符:
in the vertical layout, add the separator in the following way: