开发者

Java GridLayout

Is there a way to add elements to a gridlayout that adds them horizontally instead of vertically? Basical开发者_如何学Pythonly i want to fill up an entire row with elements before it begins with the next.

Hope i made my question clear.


That is how GridLayout works.


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);


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜