FlowLayout :JPanel的默认布局管理器 : 流布局 « Swing « Java 教程

En
Java 教程
1. 语言基础
2. 数据类型
3. 操作符
4. 流程控制
5. 类定义
6. 开发相关
7. 反射
8. 正则表达式
9. 集合
10. 线
11. 文件
12. 泛型
13. 本土化
14. Swing
15. Swing事件
16. 二维图形
17. SWT
18. SWT 二维图形
19. 网络
20. 数据库
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web服务SOA
27. EJB3
28. Spring
29. PDF
30. 电子邮件
31. 基于J2ME
32. J2EE应用
33. XML
34. 设计模式
35. 日志
36. 安全
37. Apache工具
38. 蚂蚁编译
39. JUnit单元测试
Java
Java 教程 » Swing » 流布局 
14. 89. 4. FlowLayout :JPanel的默认布局管理器
  1. A FlowLayout adds components to the container in rows.
  2. When it can't fit more components in a row, it starts a new row.
  3. Components within a FlowLayout-managed container are given their preferred size.
  4. If there is insufficient space, you do not see all the components.

There are three constructors for creating the FlowLayout layout manager:

public FlowLayout()
public FlowLayout(int alignment)
public FlowLayout(int alignment, int hgap, int vgap)

If an alignment is not specified, default is centered. The setting is controlled by one of the following constants:

  1. FlowLayout.LEFT. Left-justify component rows.
  2. FlowLayout.RIGHT. Right-justify component rows.
  3. FlowLayout.CENTER. Center component rows.
  4. FlowLayout.LEADING. Justify component rows to the leading edge of the container's orientation, e.g. to the left in the left-to-right orientation.
  5. FlowLayout.TRAILING. Justify component rows to the trailing edge of the container's orientation, e.g. to the right in the left-to-right orientation.
  1. You can specify the gaps, in pixels, both horizontal (hgap) and vertical (vgap).
  2. The horizontalGap argument determines the distance between two components in the same row and between the components and the container border.
  3. The verticalGap argument determines the distance between components in adjacent rows and the components and the container border.
  4. The default for both horizontalGap and verticalGap is 5 unit.
  5. Negative gaps place component on top of one another.
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;

public class FlowLayoutComponentOrientationRIGHT_TO_LEFT extends JFrame {

  public FlowLayoutComponentOrientationRIGHT_TO_LEFT() {
    getContentPane().setLayout(new FlowLayout());
    getContentPane().add(new JButton("OK"));
    getContentPane().add(new JButton("Cancel"));

    applyOrientation(this, ComponentOrientation.RIGHT_TO_LEFT);
  }

  public static void main(String[] argv) {
    JFrame frame = new FlowLayoutComponentOrientationRIGHT_TO_LEFT();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }

  private void applyOrientation(Component c, ComponentOrientation o) {
    c.setComponentOrientation(o);

    if (instanceof JMenu) {
      JMenu menu = (JMenuc;
      int ncomponents = menu.getMenuComponentCount();
      for (int i = 0; i < ncomponents; ++i) {
        applyOrientation(menu.getMenuComponent(i), o);
      }
    else if (instanceof Container) {
      Container container = (Containerc;
      int ncomponents = container.getComponentCount();
      for (int i = 0; i < ncomponents; ++i) {
        applyOrientation(container.getComponent(i), o);
      }
    }
  }
}
14. 89. 流布局
14. 89. 1. Laying Out Components in a Flow (Left-to-Right, Top-to-Bottom)
14. 89. 2. Three constructors available for the FlowLayout manager.
14. 89. 3. 布局管理器
14. 89. 4. FlowLayout :JPanel的默认布局管理器
14. 89. 5. FlowLayout行为FlowLayout行为
14. 89. 6. 使用FlowLayout使用FlowLayout
14. 89. 7. 改变距离改变距离
14. 89. 8. Setting the gaps between components and rows explicitly by calling the setHgap()Setting the gaps between components and rows explicitly by calling the setHgap()
14. 89. 9. 解决对齐问题解决对齐问题
14. 89. 10. 使用FlowLayout排列复选框,标签和TextField
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.