GUI编程之BorderLayout

/*
 测试setBounds
 public void setBounds(int x, int y, int width, int height);
  移动组件并指定其大小。
  
  BorderLayout布局管理器 边缘布局管理器。东西南北中。边缘布局管理器将整个容器的布局划分成东西南北中。组件只能被添加到指定的区域。
  如果不指定组件的加入部位,则默认加入到CENTER区
  每个区域只能添加一个组件,如加入多个,则先前加入的组件会被覆盖。这句话可以通过程序的倒数第二句b3把p2覆盖得到证明。
  并且通过验证,这五个区域的组件必须唯一,不能相同。
*/
import java.awt.*;
public class TestPanel {
 public static void main(String[] args) {
  Frame f = new Frame("Java Frame with Panel");
  
  Panel p = new Panel();
  f.setLayout(new BorderLayout());
  f.setBounds(20, 20, 500, 500);
  p.setBounds(50, 50, 100, 100);
  f.setBackground(Color.ORANGE);
  p.setBackground(Color.PINK);
  f.add(p, BorderLayout.WEST);
  
  for (int i=0; i<4; i++) {
   p.add(new Button("b" + i));
  }
  Button b = new Button("b1");
  f.add(b, BorderLayout.EAST);
  f.add(new Button("b2"), BorderLayout.NORTH);
  Panel p2 = new Panel();
  p2.setBackground(Color.BLACK);
  f.add(p2, BorderLayout.SOUTH);
  f.add(new Button("b3"), BorderLayout.SOUTH);
  f.setVisible(true);
 }
}


你可能感兴趣的:(GUI编程)