Java 中的 GridLayout 帮助
我有一个包含以下代码的 JPanel:
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(3, 2, 10, 30));
final JTextField fileName = new JTextField();
pane.add(fileName);
JButton creater = new JButton("Create File");
pane.add(creater);
JButton deleter = new JButton("Delete File");
pane.add(deleter);
我想知道如何使 JTextField 占用 GridLayout 上的两个空格,同时让两个按钮通过在同一行上各占用一个空格来共享一行?
I have a JPanel with the following code:
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(3, 2, 10, 30));
final JTextField fileName = new JTextField();
pane.add(fileName);
JButton creater = new JButton("Create File");
pane.add(creater);
JButton deleter = new JButton("Delete File");
pane.add(deleter);
I was wondering, how do I make it so that the JTextField takes up two spaces on the GridLayout, while having the two buttons share a row by taking up one space each on the same line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 GridLyout 很难做到这一点。您已经创建了更宽的单元格(例如
new GridLayout(2, 2, 10, 30)
,然后将 TextField 添加到第一个单元格。然后您必须使用 GridLayout(2, 1) 创建另一个面板,将其放入第二行的单元格中,并将按钮添加到此嵌套网格布局的第一个单元格中。很快您就需要将 GridLayout 放入其他 GridLayout 中,
首先看看 GridBagLayout 。确保生活并不总是挑剔的:)。然后看看 MigLayout 等替代解决方案。它不是 JDK 的一部分,但它确实是强大的工具,可以让您的生活更轻松。
It is a hard to do with GridLyout. You have create wider cells (e.g.
new GridLayout(2, 2, 10, 30)
, then add TextField to the fist cell. Then you have to create yet another panel with GridLayout(2, 1), put it into the cell in second line and add your button into 1 st cell of this nested grid layout.Shortly you need GridLayout into other GridLayout.
There are better tools to implement this. First take a look on GridBagLayout. It is just to be sure that life is not always pick-nick :). Then take a look on alternative solutions like MigLayout. It is not a part of JDK but it really powerful tool that makes your life easier.
您无法使用 GridLayout 实现列跨度。我建议您尝试 GridBagLayout 和 GridBagConstraints
You cannot do column spans with GridLayout. I recomend you try GridBagLayout and GridBagConstraints
在废弃了第 3 方布局的建议之后,并且由于我对 GBL 怀有恶意的仇恨,我认为是时候“把我的代码放在我的嘴上”以接受公众监督(和废弃)。
此 SSCCE 使用嵌套布局。
After trashing the suggestion of 3rd party layouts, and since I possess a malevolent hatred of GBL, I thought it was about to time to 'put my code where my mouth is' for public scrutiny (and trashing).
This SSCCE uses a nested layout.
查看如何使用 GridBagLayout 的教程。
示例代码:
Take a look at the tutorial on How to Use GridBagLayout.
Sample code: