我的 GridLayout 的问题
我有一个 2 行 x 5 列的 GridLayout,我想将第一行的高度设置为 50,第二行的高度设置为 200。我知道 GridLayout 创建相同大小的单元格,所以这不起作用。我还尝试将两行拆分为两个 GridLayout,设置其所需的高度并将它们添加到 FlowLayout,但列没有按照我想要的方式对齐。我的代码是这样的:
row1.setSize(WIDTH, 50); //GridLayout
row2.setSize(WIDTH, 200); //GridLayout
panel.add(row1);
panel.add(row2); //panel is a FlowLayout
列对齐非常重要,但我似乎无法做到这一点。
I have a GridLayout 2 rows by 5 columns, and I want to make the height of the first row to something like 50, and the second row to 200. I know GridLayout creates equally-sized cells, so this didn't work out. I also tried splitting the two rows into two GridLayouts setting their desired heights and adding them to a FlowLayout, but the columns didn't align the way I wanted it to. My code went something like this:
row1.setSize(WIDTH, 50); //GridLayout
row2.setSize(WIDTH, 200); //GridLayout
panel.add(row1);
panel.add(row2); //panel is a FlowLayout
The columns aligning are very important and I can't seem to get this right.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会研究 GridBagLayout。尽管如此,几乎所有与我交谈过的人似乎都不喜欢它。不过我喜欢它。
I'd look into GridBagLayout. Although, almost everyone I've talked seems to dislike it. I like it, though.
你不能用 GridLayout 来做到这一点。
您应该能够使用 GridBagLayout 或 SpringLayout。查看 布局管理器 上的 Swing 教程,获取一些示例你开始了。
此外,您也不会将单独的行添加到布局中。您需要使用您选择的任何布局管理器将所有 10 个组件单独添加到同一面板。
You can't do this with a GridLayout.
You should be able to use either a GridBagLayout or a SpringLayout. Check out the Swing tutorial on Layout Managers for some examples to get you started.
Also you don't add individual rows to the layout. You need to add all 10 components individually to the same panel using whatever layout manager you choose.
GridLayout 中的每个单元格的大小完全相同,因此您必须使用不同的布局管理器,例如GridBagLayout。
Each cell in a GridLayout is exactly the same size, so you'll have to use a different layout manager, e.g. GridBagLayout.