Java 网格布局

发布于 2024-08-14 01:16:42 字数 84 浏览 2 评论 0原文

有没有一种方法可以将元素添加到网格布局中,从而水平而不是垂直添加它们?基本上我想在开始下一个之前用元素填充整行。

希望我说清楚了我的问题。

Is there a way to add elements to a gridlayout that adds them horizontally instead of vertically? Basically i want to fill up an entire row with elements before it begins with the next.

Hope i made my question clear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

好久不见√ 2024-08-21 01:16:42

我建议 GridBagLayoutGridBagConstraints

它很像 GridLayout,但带有 GridBagConstraints您可以指定组件的 x 和 y 坐标,并且可以进行列和行跨度。

它看起来像这样:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

GridBagLayout layout = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();

JPanel panel = new JPanel();
JLabel label = new JLabel("foo");

panel.setLayout(layout);

c.gridx = 0;
c.gridy = 0;
panel.add(label, c);

I would suggest GridBagLayout and GridBagConstraints

It's a lot like a GridLayout, but with GridBagConstraints you can specify the x and y coordinates of the component, and you can do column and row spans.

It would look something like this:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

GridBagLayout layout = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();

JPanel panel = new JPanel();
JLabel label = new JLabel("foo");

panel.setLayout(layout);

c.gridx = 0;
c.gridy = 0;
panel.add(label, c);
爱给你人给你 2024-08-21 01:16:42

顾名思义,GridLayout 根据您在 构造函数,一旦添加了指定数量的组件,就会移动到下一行。

从你的问题来看, FlowLayout 似乎是更符合您正在寻找的内容。

编辑:我不知道为什么,但如果您将行数指定为0(例如new GridLayout(0, 9)),它似乎可以正常工作。

The GridLayout, as the name suggests lays out components according to the number of columns and rows you specified in the constructor, and will move to the next row as soon as you have added the specified number of components.

From your question, it seems that a FlowLayout is more along the lines of what you're looking for.

Edit: I'm not sure why, but if you specify the number of rows as 0 (e.g. new GridLayout(0, 9), it seems to work properly.

浅笑依然 2024-08-21 01:16:42

实际上你应该使用 GroupLayout 它是新的(从jdk 1.6开始)并且非常棒。它为您提供了很大的布局灵活性。

Actually you should use GroupLayout It's new (since jdk 1.6) and pretty awesome. It gives you a ton of flexibility in layout.

萝莉病 2024-08-21 01:16:42

我假设您的意思是说您想要在移动到下一列之前填充一列中的所有行。如果是这样,则使用自定义布局管理器

垂直网格布局

I assume you meant to say that you want to fill up all the rows in a column before moving to the next column. If so, then use a custom layout manager

VerticalGridLayout

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文