关于制作某种格式的JAVA GUI的问题
我正在尝试制作一个看起来像这样的 GUI:
我只知道如何使用具有 5 空间的 BorderLayout按钮。北、西、中、东、南。
由于我需要在顶线上有 6 个组件,因此这种方法行不通。我不知道如何制作才能在第一行拥有超过 1 个组件。是否有其他我可以使用的布局,或者是否有某种方法可以操纵 BorderLayout,以便我可以将 6 个组件放在顶行?
I am trying to make a GUI that looks something like this:
I only know how to use the BorderLayout which has space for 5 buttons. North, West, Center, East, and South.
Since I need to have 6 components on the top line, this approach can't work. I'm not sure how to make it so that I can have more than 1 component on the top line. Are there other layouts that I can use or is there some way I can manipulate BorderLayout so that I can put 6 components on the top line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要做的是将组件嵌套在其他组件内。例如,顶部(北)应该是一个
JPanel
。该JPanel
将包含顶部的 6 个组件。该代码可能类似于以下内容:
Center 组件可能是另一个包含两个中心按钮的
JPanel
。 South 组件将是另一个包含单个JLabel
或仅包含JLabel
的JPanel
。如果您不必为主面板使用
BorderLayout
,那么使用BoxLayout
可能会更容易。What you need to do is nest components inside of other components. For example, the top (North) should be one
JPanel
. ThatJPanel
will contain the 6 components on the top.The code may look similar to the following:
The Center component will probably be another
JPanel
containing the two center buttons. And the South component will be anotherJPanel
containing the singleJLabel
or simply theJLabel
.If you don't have to use a
BorderLayout
for the main panel, it may be easier to use aBoxLayout
.我再次转向 miglayout,绝对是最好的 Java 布局管理器。没有嵌套的 JPanel,只是使用基于字符串的约束的简单布局。
调试模式打开:
调整窗口大小后(请注意文本字段大小的比例保持不变)
Once again I turn to miglayout, the absolute best layout manager for Java. No nested JPanels, just a simple layout using string based constraints.
With debug mode on:
After resizing the window (note the ratio of the size of the textfields remains the same)
如果我要重新创建该 UI,我会从使用 3 行 1 列的 GridLayout 的 JPanel 开始。在每一列中,我将添加一个子 JPanel。
然后,对于每一行,我将使用 GridBagLayout 来定位组件。
If I were recreating that UI, I would start with a JPanel using a GridLayout with 3 rows and 1 column. In each column I would add a child JPanel.
Then for each row I would use a GridBagLayout to position the components.
这里是有关布局管理器的教程。
请记住,您始终可以向 JPanel 添加多个元素并将特定布局应用于该 JPanel。然后您可以嵌套面板(在其他面板内添加面板)。
Here is a tutorial about layout managers.
Remember that you can always add several elements to a JPanel and apply a specific layout to that JPanel. Then you can nest panels(add panels inside other panels).