1 需求
知识点:
- Window、ScrollPane:默认BorderLayout布局;
- Panel:默认FlowLayout布局;
2 接口
Class BorderLayout
java.lang.Object
java.awt.BorderLayout
- Field Detail
- public static final String NORTH
- public static final String SOUTH
- public static final String EAST
- public static final String WEST
- public static final String CENTER
- public static final String BEFORE_FIRST_LINE
- public static final String AFTER_LAST_LINE
- public static final String BEFORE_LINE_BEGINS
- public static final String AFTER_LINE_ENDS
- public static final String PAGE_START
- public static final String PAGE_END
- public static final String LINE_START
- public static final String LINE_END
- Constructor Detail
- public BorderLayout()
- public BorderLayout(int hgap, int vgap)
- Method Detail
- public int getHgap()
- public void setHgap(int hgap)
- public int getVgap()
- public void setVgap(int vgap)
- public void addLayoutComponent(Component comp, Object constraints)
- public void removeLayoutComponent(Component comp)
- public Component getLayoutComponent(Object constraints)
- public Component getLayoutComponent(Container target, Object constraints)
- public Object getConstraints(Component comp)
- public Dimension minimumLayoutSize(Container target)
- public Dimension preferredLayoutSize(Container target)
- public Dimension maximumLayoutSize(Container target)
- public float getLayoutAlignmentX(Container parent)
- public float getLayoutAlignmentY(Container parent)
- public void invalidateLayout(Container target)
- public void layoutContainer(Container target)
- public String toString()
3 示例代码
import java.awt.*;
public class Test {
public static void main(String[] args) {
Frame frame = new Frame("窗口");
frame.setLayout(new BorderLayout(20, 40));
// NORTH 示例
frame.add(new Button("北侧按钮"), BorderLayout.NORTH);
// WEST 示例
frame.add(new Button("西侧按钮"), BorderLayout.WEST);
// CENTER 示例
frame.add(new Button("中间按钮"));
// EAST 示例
frame.add(new Button("东侧按钮"), BorderLayout.EAST);
// SOUTH 示例
frame.add(new Button("南侧按钮"), BorderLayout.SOUTH);
// 最佳显示
frame.pack();
// 窗口可见
frame.setVisible(true);
}
}
注意:窗口拉伸时,NORTH和SOUTH宽度不会变化,WEST、CENTER、EAST宽度会变化。
4 参考资料



