java2标准中的布局管理器有六种:前5个来自AWT,第六个来自Swing。
1.FlowLayout(int align,int hgap,int vgap),从左到右,从上到下将组件加到容器中,JPanel和Applet默认就是这种布局。
例如:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Flow extends JPanel {  public Flow(  ) {
    // FlowLayout is default layout manager for a JPanel
    add(new JButton("One"));
    add(new JButton("Two"));
    add(new JButton("Three"));
    add(new JButton("Four"));
    add(new JButton("Five"));
  }  public static void main(String[] args) {
    JFrame f = new JFrame("Flow");
    f.addWindowListener(new WindowAdapter(  ) {
      public void windowClosing(WindowEvent e) { System.exit(0); }
    });
    f.setSize(400, 75);
    f.setLocation(200, 200);
    Flow flow = new Flow(  );
    f.setContentPane(flow);
    f.setVisible(true);
  }
}
2.BorderLayout()通过调用每个组件的PreferredSize()方法,告诉组件占用的尺寸,BorderLayout把容器分为五个方向,North,South,East,West和Center,然后想容器添加<=五个控件,就回自动形成BorderLayout。
例如:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Border1 extends JPanel {  public Border1(  ) {
    setLayout(new BorderLayout(  ));
    add(new JButton("North"), BorderLayout.NORTH);
    add(new JButton("South"), BorderLayout.SOUTH);
    add(new JButton("East"), BorderLayout.EAST);
    add(new JButton("West"), BorderLayout.WEST);
    add(new JButton("Center"), BorderLayout.CENTER);
  }  public static void main(String[] args) {
    JFrame f = new JFrame("Border1");
    f.addWindowListener(new WindowAdapter(  ) {
      public void windowClosing(WindowEvent e) { System.exit(0); }
    });
    f.setSize(300, 300);
    f.setLocation(200, 200);
    f.setContentPane(new Border1(  ));
    f.setVisible(true);
  }
}
3.GridLayout(int rows,int cols)定制一种格栅布局。
例如:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Grid extends JPanel {  public Grid(  ) {
    setLayout(new GridLayout(3, 2));
    add(new JButton("One"));
    add(new JButton("Two"));
    add(new JButton("Three"));
    add(new JButton("Four"));
    add(new JButton("Five"));
  }  public static void main(String[] args) {
    JFrame f = new JFrame("Grid");
    f.addWindowListener(new WindowAdapter(  ) {
      public void windowClosing(WindowEvent e) { System.exit(0); }
    });
    f.setSize(200, 200);
    f.setLocation(200, 200);
    f.setContentPane(new Grid(  ));
    f.setVisible(true);
  }
}
4.GridBagLayout是GridLayout的衍生物,它强制在一个格栅位置装配控件,允许控件占用几个相邻的格栅位置公空间,采用一系列约束和权值是控件各就各位。下面给出一个例子,具体的参数和用法请参考书籍和JFC文档。
例如:
//file: GridBag2.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class GridBag2 extends JPanel {
  GridBagConstraints constraints = new GridBagConstraints(  );  public GridBag2(  ) {
    setLayout(new GridBagLayout(  ));
    constraints.weightx = 1.0;
    constraints.weighty = 1.0;
    constraints.fill = GridBagConstraints.BOTH;
    int x, y;  // for clarity
    addGB(new JButton("North"),  x = 1, y = 0);
    addGB(new JButton("West"),   x = 0, y = 1);
    addGB(new JButton("Center"), x = 1, y = 1);
    addGB(new JButton("East"),   x = 2, y = 1);
    addGB(new JButton("South"),  x = 1, y = 2);
  }  void addGB(Component component, int x, int y) {
    constraints.gridx = x;
    constraints.gridy = y;
    add(component, constraints);
  }  public static void main(String[] args) {
    JFrame f = new JFrame("GridBag2");
    f.addWindowListener(new WindowAdapter(  ) {
      public void windowClosing(WindowEvent e) { System.exit(0); }
    });
    f.setSize(225, 150);
    f.setLocation(200, 200);
    f.setContentPane(new GridBag2(  ));
    f.setVisible(true);
  }
}
//file: GridBag3.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class GridBag3 extends JPanel {
  GridBagConstraints constraints = new GridBagConstraints(  );  public GridBag3(  ) {
    setLayout(new GridBagLayout(  ));
    constraints.weightx = 1.0;
    constraints.weighty = 1.0;
    constraints.fill = GridBagConstraints.BOTH;
    int x, y;  // for clarity
    constraints.gridheight = 2; // span two rows
    addGB(new JButton("one"),   x = 0, y = 0);
    constraints.gridheight = 1; // set it back
    addGB(new JButton("two"),   x = 1, y = 0);
    addGB(new JButton("three"), x = 2, y = 0);
    constraints.gridwidth = 2; // span two columns
    addGB(new JButton("four"),  x = 1, y = 1);
    constraints.gridwidth = 1; // set it back
  }  void addGB(Component component, int x, int y) {
    constraints.gridx = x;
    constraints.gridy = y;
    add(component, constraints);
  }  public static void main(String[] args) {
    JFrame f = new JFrame("GridBag3");
    f.addWindowListener(new WindowAdapter(  ) {
      public void windowClosing(WindowEvent e) { System.exit(0); }
    });
    f.setSize(200, 200);
    f.setLocation(200, 200);
    f.setContentPane(new GridBag3(  ));
    f.setVisible(true);
  }
}
5.AWT中最后一个是CardLayout管理器,它完成的工作与索查标签窗格的工作完全相同,只不过不太精细,现在没有理由在使用它了,因为有了索查标签窗格。
6.Swing中的BoxLayout,它能够水平或垂直地对齐一组控件。
最后给出一个BoxLayout地例子:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class ButtonDemo2 {    static JFrame frame = new JFrame("Example");    public static void setupFrame() {
        frame.setSize(400,100);
        frame.setVisible(true);
        frame.getContentPane().setLayout( new FlowLayout() );        WindowListener l = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        };
        frame.addWindowListener(l);
    }    public static void main(String[] args) {
        setupFrame();
        Icon spIcon = new ImageIcon("spam.jpg");
        final JButton jb = new JButton("press here for Spam", spIcon);        jb.addActionListener( new ActionListener() {
                int i = 1;
                public void actionPerformed(ActionEvent e) 
                {  // this sets the size to a literal, make it variable
                   // jb.setSize(100,10);
                   System.out.println("delivering spam "+ i++); }
              } );        frame.getContentPane().add( jb );
        frame.pack();
    }
}

解决方案 »

  1.   

    实际应用中是使用以上介绍地几种Layout形成组合布局。
    例如下面是一个计算器程序地GUI界面地布局,综合利用了GridBagLayout和JFrame默认地BorderLayout.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class Calculator extends JPanel implements ActionListener {
      GridBagConstraints gbc = new GridBagConstraints(  );
      JTextField theDisplay = new JTextField(  );  public Calculator(  ) {
        gbc.weightx = 1.0;  gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        ContainerListener listener = new ContainerAdapter(  ) {
          public void componentAdded(ContainerEvent e) {
            Component comp = e.getChild(  );
            if (comp instanceof JButton)
              ((JButton)comp).addActionListener(Calculator.this);
          }
        };
        addContainerListener(listener);
        gbc.gridwidth = 4;
        addGB(this, theDisplay, 0, 0);
        // make the top row
        JPanel topRow = new JPanel(  );
        topRow.addContainerListener(listener);
        gbc.gridwidth = 1;
        gbc.weightx = 1.0;
        addGB(topRow, new JButton("C"), 0, 0);
        gbc.weightx = 0.33;
        addGB(topRow, new JButton("%"), 1, 0);
        gbc.weightx = 1.0;
        addGB(topRow, new JButton("+"), 2, 0 );
        gbc.gridwidth = 4;
        addGB(this, topRow, 0, 1);
        gbc.weightx = 1.0;  gbc.gridwidth = 1;
        // make the digits
        for(int j=0; j<3; j++)
            for(int i=0; i<3; i++)
                addGB(this, new JButton("" + ((2-j)*3+i+1) ), i, j+2);
        // -, x, and divide
        addGB(this, new JButton("-"), 3, 2);
        addGB(this, new JButton("x"), 3, 3);
        addGB(this, new JButton("\u00F7"), 3, 4);
        // make the bottom row
        JPanel bottomRow = new JPanel(  );
        bottomRow.addContainerListener(listener);
        gbc.weightx = 1.0;
        addGB(bottomRow, new JButton("0"), 0, 0);
        gbc.weightx = 0.33;
        addGB(bottomRow, new JButton("."), 1, 0);
        gbc.weightx = 1.0;
        addGB(bottomRow, new JButton("="), 2, 0);
        gbc.gridwidth = 4;
        addGB(this, bottomRow, 0, 5);
      }  void addGB(Container cont, Component comp, int x, int y) {
        if ((cont.getLayout(  ) instanceof GridBagLayout) == false)
          cont.setLayout(new GridBagLayout(  ));
        gbc.gridx = x; gbc.gridy = y;
        cont.add(comp, gbc);
      }  public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand(  ).equals("C"))
          theDisplay.setText("");
        else
          theDisplay.setText(theDisplay.getText(  )
                             + e.getActionCommand(  ));
      }  public static void main(String[] args) {
        JFrame f = new JFrame("Calculator");
        f.addWindowListener(new WindowAdapter(  ) {
          public void windowClosing(WindowEvent e) { System.exit(0); }
        });
        f.setSize(200, 250);
        f.setLocation(200, 200);
        f.setContentPane(new Calculator(  ));
        f.setVisible(true);
      }
    }
      

  2.   

    楼上的狠,贴了这么多。楼主到www。java-cn。com当本《精通swing程序设计》,里面有章节专门讲布局管理器。