使 2 个 JButton 大小相等
我有两个带有文本“确定”和“取消”的 JButton
。我正在使用 GridBagLayout 来将它们在 JDialog 中对齐。我已将锚点设置为 GridBagConstraints.CENTER 。由于文本“确定”和“取消”的字符数不同,按钮的大小也不同。如何正确对齐它们以使它们具有相同的尺寸。我尝试了以下但无济于事。
okayButton.setSize(cancelButton.getSize());
I have two JButton
s with texts "Ok" and "Cancel". I am using GridBagLayout
to align them in a JDialog
. I have set the anchor to GridBagConstraints.CENTER
. Due to the difference in the number of characters in the texts "Ok" and "Cancel", the buttons are of different sizes. How do I align them correctly so that each of them have the same size. I tried the following but no avail.
okayButton.setSize(cancelButton.getSize());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将填充设置为 GridBagConstraints.BOTH 并赋予两个按钮相同的权重。
Try setting the fill to
GridBagConstraints.BOTH
and give both buttons equal weight.GridBaglayout 已获得 GridBagConstraints 并在所有情况下接受
PreferredSize
示例此处和此处
GridBaglayout have got GridBagConstraints and in all cases accepts
PreferredSize
examples here and here
而不是
okayButton.setSize(cancelButton.getSize());
使用okayButton.setPreferredSize(cancelButton.getPreferredSize());
Instead of
okayButton.setSize(cancelButton.getSize());
use
okayButton.setPreferredSize(cancelButton.getPreferredSize());