有没有更简单的方法来显示这个 Java UI?
我想显示一行文本,其下方有一个按钮,两者水平和垂直居中。我目前有一个 BoxLayout
,其中包含一个 JLabel
、一个用于间距的刚性区域,然后是 JPanel
内的一个 JButton
。对于我想要做的事情来说,这似乎相当复杂,而且当窗口大小改变时,文本和按钮都不再居中。有更好的方法吗?
----------------------
| |
| |
| |
| ________ |
| | Text | |
| -------- |
| ________ |
| |Button| |
| -------- |
| |
| |
| |
----------------------
I want to display a line of text with a button underneath it, both of which are centered horizontally and vertically. I currently have a BoxLayout
containing a JLabel
, a rigid area for spacing and then a JButton
within a JPanel
. This seems quite complicated for what I want to do and also when the window size is changed, both the text and the button are no longer centered. Is there a better way to do this?
----------------------
| |
| |
| |
| ________ |
| | Text | |
| -------- |
| ________ |
| |Button| |
| -------- |
| |
| |
| |
----------------------
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会选择这样的内容:
它仍然有点复杂,您可能想要为标签添加另一个面板,以便它居中,根据我的经验,UI 的布局往往需要大量工作。
I'd go with something like:
It's still a bit complex, and you'd probably want to add another panel for the label so that it's centered, UIs in my experience tend to be a lot of work to layout.
更新
Update
使用 2x1 GridLayout。
Use a 2x1 GridLayout.