这是VerticalFlowLayout的示例。它是FlowLayout类的副本,其中某些逻辑已更改为“垂直”而不是“水平”:
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class VerticalFlowLayout implements LayoutManager, java.io.Serializable{ public static final int TOP = 0; public static final int CENTER = 1; public static final int BOTTOM = 2; int align; // This is the one we actually use int hgap; int vgap; public VerticalFlowLayout() { this(CENTER, 5, 5); } public VerticalFlowLayout(int align) { this(align, 5, 5); } public VerticalFlowLayout(int align, int hgap, int vgap) { this.hgap = hgap; this.vgap = vgap; setAlignment(align); } public int getAlignment() { return align; } public void setAlignment(int align) { this.align = align; } public int getHgap() { return hgap; } public void setHgap(int hgap) { this.hgap = hgap; } public int getVgap() { return vgap; } public void setVgap(int vgap) { this.vgap = vgap; } public void addLayoutComponent(String name, Component comp) { } public void removeLayoutComponent(Component comp) { } public Dimension preferredLayoutSize(Container target) { synchronized (target.getTreeLock()) { Dimension dim = new Dimension(0, 0); int nmembers = target.getComponentCount(); boolean firstVisibleComponent = true; for (int i = 0 ; i < nmembers ; i++) { Component m = target.getComponent(i); if (m.isVisible()) { Dimension d = m.getPreferredSize(); dim.width = Math.max(dim.width, d.width); if (firstVisibleComponent) { firstVisibleComponent = false; } else { dim.height += vgap; } dim.height += d.height; } } Insets insets = target.getInsets(); dim.width += insets.left + insets.right + hgap*2; dim.height += insets.top + insets.bottom + vgap*2; return dim; } } public Dimension minimumLayoutSize(Container target) { synchronized (target.getTreeLock()) { Dimension dim = new Dimension(0, 0); int nmembers = target.getComponentCount(); boolean firstVisibleComponent = true; for (int i = 0 ; i < nmembers ; i++) { Component m = target.getComponent(i); if (m.isVisible()) { Dimension d = m.getMinimumSize(); dim.width = Math.max(dim.width, d.width); if (firstVisibleComponent) { firstVisibleComponent = false; } else { dim.height += vgap; } dim.height += d.height; } } Insets insets = target.getInsets(); dim.width += insets.left + insets.right + hgap*2; dim.height += insets.top + insets.bottom + vgap*2; return dim; } } public void layoutContainer(Container target) { synchronized (target.getTreeLock()) { Insets insets = target.getInsets(); int maxHeight = target.getSize().height - (insets.top + insets.bottom + vgap*2); int nmembers = target.getComponentCount(); int x = insets.left + hgap; int y = 0; int columnWidth = 0; int start = 0; boolean ttb = target.getComponentOrientation().isLeftToRight(); for (int i = 0 ; i < nmembers ; i++) { Component m = target.getComponent(i); if (m.isVisible()) { Dimension d = m.getPreferredSize(); m.setSize(d.width, d.height); if ((y == 0) || ((y + d.height) <= maxHeight)) { if (y > 0) { y += vgap; } y += d.height; columnWidth = Math.max(columnWidth, d.width); } else { moveComponents(target, x, insets.top + vgap, columnWidth, maxHeight - y, start, i, ttb); y = d.height; x += hgap + columnWidth; columnWidth = d.width; start = i; } } } moveComponents(target, x, insets.top + vgap, columnWidth, maxHeight - y, start, nmembers, ttb); } } private void moveComponents( Container target, int x, int y, int width, int height, int columnStart, int columnEnd, boolean ttb) { switch (align) { case TOP: y += ttb ? 0 : height; break; case CENTER: y += height / 2; break; case BOTTOM: y += ttb ? height : 0; break; } for (int i = columnStart ; i < columnEnd ; i++) { Component m = target.getComponent(i); if (m.isVisible()) { int cx; cx = x + (width - m.getSize().width) / 2; if (ttb) { m.setLocation(cx, y); } else { m.setLocation(cx, target.getSize().height - y - m.getSize().height); } y += m.getSize().height + vgap; } } } public String toString() { String str = ""; switch (align) { case TOP: str = ",align=top"; break; case CENTER: str = ",align=center"; break; case BOTTOM: str = ",align=bottom"; break; } return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + str + "]"; } public static void main(String[] args) { JPanel main = new JPanel( new BorderLayout() ); final JPanel buttons = new JPanel(new VerticalFlowLayout() );// buttons.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); main.add(buttons, BorderLayout.CENTER); for (int i = 0; i < 7; i++) { buttons.add( new JRadioButton("button " + i) ); } JButton button = new JButton("Add Radio Button"); main.add(button, BorderLayout.SOUTH); button.addActionListener( new ActionListener() { private int i = 8; public void actionPerformed(ActionEvent e) { buttons.add( new JRadioButton("button R Us" + i++) ); buttons.revalidate();// pack(); } }); Jframe frame = new Jframe(); frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); frame.add(main); frame.setSize(300, 300); frame.setLocationRelativeTo(null); frame.setVisible(true); }}从理论上讲,您应该能够使用WrapLayout并使它扩展此类,然后将代码自定义为也垂直。
编辑:
我有WrapLayout扩展VerticalFlowLayout这样的:
You can’t just extend the VerticalFlowLayout. The WrapLayout pre is designed
to calculate a fixed width based on the size of the parent container. You need
to change the behaviour the calculate a fixed height. I haven’t tried it but
basically you would need to change “width” related variable references to
“height” and “height” related variable references to “width” so that the pre
will work on the vertical dimension instead of the horizontal dimension.



